开发者

how to add all of these values in a python dictionary

开发者 https://www.devze.com 2022-12-14 01:26 出处:网络
last one for the night, want to see what clever ways there are with python to add all of the \'count\' values from the following type 开发者_如何学编程of dictionary:

last one for the night, want to see what clever ways there are with python to add all of the 'count' values from the following type 开发者_如何学编程of dictionary:

{0: {'count': 1000}, 1: {'count': 2000}}

so the end result should be an int value of 3000.


>>> x = {0: {'count': 1000}, 1: {'count': 2000}}
>>> sum(v['count'] for v in x.values()) 
3000


A shorter one:

sum(d[k]['count'] for k in d)


sum(i['count'] for i in d.values())


How about using reduction in python?

reduce(lambda x,y: x+y, [v['count'] for v in a.values()])
0

精彩评论

暂无评论...
验证码 换一张
取 消