Python - 在字典中求和值 [英] Python - sum values in dictionary
本文介绍了Python - 在字典中求和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的列表:
I have got pretty simple list:
example_list = [
{'points': 400, 'gold': 2480},
{'points': 100, 'gold': 610},
{'points': 100, 'gold': 620},
{'points': 100, 'gold': 620}
]
我如何总结所有黄金值?我正在寻找不错的单线.
How can I sum all gold values? I'm looking for nice oneliner.
现在我正在使用此代码(但这不是最佳解决方案):
Now I'm using this code (but it's not the best solution):
total_gold = 0
for item in example_list:
total_gold += example_list["gold"]
推荐答案
sum(item['gold'] for item in myList)
这篇关于Python - 在字典中求和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文