简体   繁体   中英

Get keys with maximum value from a nested dictionary

Thanks in advance for your help.

I built the following code (I tried the below, I used a dictionary within a dictionary).

import operator
character = {'eyes_color':{"blue":10,"brown":12},
         'hair_color':{"black":15, "blonde":7},
         'gender': {"male":16,"female":6}
         }
maximun_key=max(character.items(), key=operator.itemgetter(1))[0]

As you can see, I used in my code:

maximun_key=max(character.items(), key=operator.itemgetter(1))[0]

Getting as output:

 brown male black 

ie the maximum, but for each dictionary .

I expected for this case an output like:

male

I mean the keys with maximum values.

Does anyone know how I can solve this problem?

You can do this using map and functools.partial as well.

vmax = partial(max, key=itemgetter(1))
vmax(map(vmax, map(dict.items, character.values())))[0]

This uses partial to make a reusable max function with a custom key, then just map dict.items to each sub dict, and the max to that then just get the max of those results.

>>> from operator import itemgetter
>>> from functools import partial
>>> character = {'eyes_color':{"blue":10,"brown":12}, 'hair_color':{"black":15, "blonde":7}, 'gender': {"male":16,"female":6}}
>>> vmax = partial(max, key=itemgetter(1))
>>> max(map(vmax, map(dict.items, character.values())))[0]
male 

An alternative way -

Using lambda :

character = {'eyes_color':{"blue":10,"brown":12},
         'hair_color':{"black":15, "blonde":7},
         'gender': {"male":16,"female":6}
         }

maximun_key= max([max(chars.items(),key = lambda x: x[1]) for chars in character.values()],key = lambda x: x[1])[0]

OR

Using operator.itemgetter :

from operator import itemgetter
character = {'eyes_color':{"blue":10,"brown":12},
         'hair_color':{"black":15, "blonde":7},
         'gender': {"male":16,"female":6}
         }
maximun_key= max([max(chars.items(),key=itemgetter(1)) for chars in character.values()],key=itemgetter(1))[0]

Use the below code to get the result:-

import numpy as np
key_list, value_list = [], []
character = {'eyes_color':{"blue":10,"brown":12},
     'hair_color':{"black":15, "blonde":7},
     'gender': {"male":16,"female":6}
     }

for var in character.values():
    for i in var :
        key_list.append(i)
        value_list.append(var[i])

max_index = np.argmax(value_list)   #Getting index of maximum value
result = key_list[max_index]  #Getting result with the help of index value.
print(result)

Output

male

In simple way with builtin features:

d = {'eyes_color': {"blue": 10, "brown": 12},
     'hair_color': {"black": 15, "blonde": 7},
     'gender': {"male": 16, "female": 6}
     }

max_value, max_key = max(((v,k) for inner_d in d.values() for k,v in inner_d.items()))
print(max_key)   # male
print(max_value) # 16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM