【发布时间】:2017-01-06 21:29:59
【问题描述】:
给定一个字典列表,我如何返回总和最大的字典
>>>testing
[{'key1': 1, 'key2': 14, 'key3': 47},
{'key1': 222, 'key2': 222, 'key3': 222},
{'key1': 0, 'key2': 0, 'key3': 0}]
我想回来
{'key1': 222, 'key2': 222, 'key3': 222}
到目前为止,我已经尝试过[sum(i.itervalues()) for i in testing],它会告诉我列表中的哪些项目具有最大值,但我不知道如何返回列表。我正在使用 Python 2.7 fwiw。
【问题讨论】:
-
max(testing, key=lambda x: sum(x.itervalues()))
标签: python python-2.7 list dictionary