Get the Key Corresponding to the Minimum Value Within a Dictionary

Get the key corresponding to the minimum value within a dictionary

Best: min(d, key=d.get) -- no reason to interpose a useless lambda indirection layer or extract items or keys!

>>> d = {320: 1, 321: 0, 322: 3}
>>> min(d, key=d.get)
321

Get the key corresponding to the 3 minimum values within a dictionary

Try this:

l = [item for item in sorted(d, key=d.get)][0:3]
print(l)

Output

[1, 0.1, 0.3]

How to return a list of keys corresponding to the smallest value in dictionary

Can do it as a two-pass:

>>> colour
{'blue': 5, 'purple': 6, 'green': 2, 'red': 2}
>>> min_val = min(colour.itervalues())
>>> [k for k, v in colour.iteritems() if v == min_val]
['green', 'red']
  1. Find the min value of the dict's values
  2. Then go back over and extract the key where it's that value...

An alternative (requires some imports, and means you could take the n many if wanted) - this code just takes the first though (which would be the min value):

from itertools import groupby
from operator import itemgetter

ordered = sorted(colour.iteritems(), key=itemgetter(1))
bykey = groupby(ordered, key=itemgetter(1))
print map(itemgetter(0), next(bykey)[1])
# ['green', 'red']

Retrieve keys corresponding to minimum value in nested dict of dicts

One way is to restructure your dictionary and use a list comprehension to retrieve the keys with minimum value:

d = {
3: {5: 0.6, 37: 0.98, 70: 0.36},
5: {5: 2.67, 37: 0.01, 70: 0.55},
7: {5: 0.2, 37: 0.3, 70: 1.2}
}

res_dict = {}

for k, v in d.items():
for k2, v2 in v.items():
res_dict[(k, k2)] = v2

minval = min(res_dict.values())
res = [k for k, v in res_dict.items() if v == minval]

print(res)
# [(5, 37)]

How to find the key of a minimum value in a nested dictionary?

You can reverse the keys and the values, then obtain the key with the minimum value:

 a = {0: {0: 7, 1: 2, 2: 5}, 1: {0: 3, 1: 10, 2: 10}}
dict(zip(a[0].values(),a[0].keys())).get(min(a[0].values()))

here we create a new dictionary whose keys and values are the reverse of the original dictionary. eg

dict(zip(a[0].values(),a[0].keys()))
Out[1575]: {7: 0, 2: 1, 5: 2}

Then from here, we obtain the minimum value in the original dictionary and use that as the key in this reversed dictionary

EDIT

As indicated in the comments, one can simply use the key within the min function:

   min(a[0],key = a[0].get)

return key of minimum value

Use min with a lambda lookup function:

min(d, key=lambda k: d[k][1])

Python: get key with the least value from a dictionary BUT multiple minimum values

One simple option is to first determine the minimum value, and then select all keys mapping to that minimum:

min_value = min(d.itervalues())
min_keys = [k for k in d if d[k] == min_value]

For Python 3 use d.values() instead of d.itervalues().

This needs two passes through the dictionary, but should be one of the fastest options to do this anyway.

Using reservoir sampling, you can implement a single pass approach that selects one of the items at random:

it = d.iteritems()
min_key, min_value = next(it)
num_mins = 1
for k, v in it:
if v < min_value:
num_mins = 1
min_key, min_value = k, v
elif v == min_value:
num_mins += 1
if random.randrange(num_mins) == 0:
min_key = k

After writing down this code, I think this option is of rather theoretical interest… :)

getting minimum (key, value) in a list that holds a dictionary

If you want the min for each dict in the list, you can use the following list comprehension:

[min(d.items(), key=lambda x: x[1]) for d in queue]

Which for your example returns:

[(1, 0.39085439023582913)]

d.items() returns a list of tuples in the form (key, value) for the dictionary d. We then sort these tuples using the value (x[1] in this case).

If you always have your data in the form of a list with one dictionary, you could also call .items() on the first element of queue and find the min:

print(min(queue[0].items(), key=lambda x:x[1]))
#(1, 0.39085439023582913)


Related Topics



Leave a reply



Submit