Python - 字典中的sum值 [英] Python - sum values in dictionary

查看:1082
本文介绍了Python - 字典中的sum值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很简单的列表:

  example_list = [
{'points':400,'gold ':2480},
{'points':100,'gold':610},
{'points':100,'gold':620},
{'points' 100,'gold':620}
]

金值?现在我正在使用这个代码(但不是最好的解决方案):

  total_gold = 0 
在example_list中的项目:
total_gold + = example_list [gold]
/ pre>

解决方案

  sum(item ['gold'] for myLIst中的项目)


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 - 字典中的sum值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆