python字典嵌套列表取最大值_在python字典中获取具有最大嵌套键的键

论坛 期权论坛 期权     
匿名技术用户   2021-1-10 19:08   57331   0

I have a dictionary like so:

my_dictionary = {

'key1': {'a': 1, 'b': 1, 'c': 10},

'key2': {'a': 1, 'b': 1, 'c': 11},

'key3': {'a': 1, 'b': 1, 'c': 12}

}

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 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'

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP