Find Maximum Value in Nested Dictionary and return Key

蓝咒 提交于 2019-12-09 06:42:24

Use float('-inf') when the key is missing:

m = max(dictionary, key=lambda v: dictionary[v].get(value, float('-inf')))

Negative infinity is guaranteed to be smaller than any existing value in the dictionaries, ensuring that nested dictionaries with the specific key missing are ignored.

Demo:

>>> dictionary = {
...   'key1': {'a': 1, 'b': 2, 'c': 10}, 
...   'key2': {'d': 1, 'e': 1, 'c': 11}, 
...   'key3': {'d': 2, 'b': 1, 'g': 12}}
>>> list1 = ('a', 'b', 'c')
>>> for value in list1:
...      print(value, max(dictionary, key=lambda v: dictionary[v].get(value, float('-inf'))))
... 
a key1
b key1
c key2

However, it'll be more efficient if you looped over all your dictionary values just once instead:

maximi = dict.fromkeys(list1, (None, float('-inf')))

for key, nested in dictionary.items():
    for k in nested.keys() & maximi:  # intersection of keys
        if maximi[k][0] is None or dictionary[maximi[k][0]][k] < nested[k]:
            maximi[k] = (key, nested[k])

for value in list1:
    print(value, maximi[value][0])

That's presuming you are using Python 3; in Python 2, replace .items() with .iteritems() and .keys() with .viewkeys().

Demo:

>>> maximi = dict.fromkeys(list1, (None, float('-inf')))
>>> for key, nested in dictionary.items():
...     for k in nested.keys() & maximi:  # intersection of keys
...         if maximi[k][0] is None or dictionary[maximi[k][0]][k] < nested[k]:
...             maximi[k] = (key, nested[k])
... 
>>> maximi
{'a': ('key1', 1), 'b': ('key1', 2), 'c': ('key2', 11)}
>>> for value in list1:
...     print(value, maximi[value][0])
... 
a key1
b key1
c key2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!