简体   繁体   English

从嵌套字典中获取具有最大值的键

[英]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. 您也可以使用mapfunctools.partial进行此操作。

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. 它使用partial通过自定义键创建可重用的max函数,然后将dict.items映射到每个子dict,然后将dict.items映射到每个子dict,然后仅获取这些结果的最大值。

>>> 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 : 使用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 : 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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