Home > Software design >  Finding key that corresponds to the lowest mean of a list of values in a dictionary
Finding key that corresponds to the lowest mean of a list of values in a dictionary

Time:04-19

I'm trying to make a function that, for each entry in a dictionary, takes the mean for every entry's list of values and returns the key that corresponds to the lowest mean. If negative values are present anywhere in the list, it should first remove them then compute the means as normally.

For example:

least_mean({'Option1': [0, 1, 2], 'Option2': [8, 9, -9999],'Option3': [0, -9999, 5, 3]})

should return 'Option1' because it has the lowest mean with a value of 1.5

My Attempt

def least_mean(string):
    empty = []
    for i in string:
        empty.append((i,sum([j for j in string[i] if j > 0])/len([j for j in string[i] if j > 0])))
    return empty

I have created a function that returns a list of tuples containing each option and their mean. However, I'm not sure how to make this function more specific to return 'Option1' by itself. For example, plugging ({'Option1': [0, 1, 2], 'Option2': [8, 9, -9999],'Option3': [0, -9999, 5, 3]}) in returns

[('Option1', 1.5), ('Option2', 8.5), ('Option3', 4.0)]

but I would like to get 'Option1' alone. If possible, could this be done in a one line list comprehension without imports?

CodePudding user response:

You can use min() with a custom key parameter to find the key corresponding to the minimum mean, ignoring negative values. No list comprehension needed:

from statistics import mean

data = {'Option1': [0, 1, 2], 'Option2': [8, 9, -9999],'Option3': [0, -9999, 5, 3]}

result = min(data, key=lambda x: mean(filter(lambda x: x >= 0, data[x])))

print(result)

If you don't want to use the statistics import, you can use:

result = min(data, key=lambda x: sum(filter(lambda x: x >= 0, data[x])) \
    / sum(1 for _ in filter(lambda x: x >= 0, data[x])))

This outputs:

Option1

If you want to add to your existing approach to extract the minimum option, use:

data = [('Option1', 1.5), ('Option2', 8.5), ('Option3', 4.0)]

result, _ = min(data, key=lambda x: x[1])
print(result)

This also outputs:

Option1
  • Related