在python字典中获取具有最大嵌套键的键 [英] Get the key with the maximum nested key in a python dictionary
本文介绍了在python字典中获取具有最大嵌套键的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的字典:
my_dictionary = {
'key1': {'a': 1, 'b': 1, 'c': 10},
'key2': {'a': 1, 'b': 1, 'c': 11},
'key3': {'a': 1, 'b': 1, 'c': 12}
}
如何比较此字典的'c'子项,找到最大的子项,并返回该字典的相应父项(在这种情况下,我想输出'key3'
,因为它的'c'
键是最高的) .谢谢!
How can I compare the 'c' subkeys of this dictionary, find the greatest one, and return the corresponding parent key of that dictionary (in this case I want to output 'key3'
as it's 'c'
key is highest). Thanks!
推荐答案
max
(可选)接受一个可调用的参数来修改比较:
max
optionally accepts a callable argument for modifying the comparison:
>>> d
{'key1': {'a': 1, 'b': 1, 'c': 10},
'key2': {'a': 1, 'b': 1, 'c': 11},
'key3': {'a': 1, 'b': 1, 'c': 12}}
>>> max(d, key=lambda v: d[v]['c'])
'key3'
这篇关于在python字典中获取具有最大嵌套键的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文