列表聚合的python字典 [英] python dictionary of list of list aggregate
问题描述
我有可用的时间序列字典,我需要计算每个键的所有值,最有效的方法是什么?
I have timeseries dictionary available, I need to count all the values of each keys, what's the most efficient way to do this?
DATA = {u'604':[[1361836800,{u'14885549':52,u'91478624':127,u'25581439':12,u'532617990':4}],[1361833200,{u'14885549':38,u'91478624':204,u'25581439':14,u'40302362':5,u'532617990':2}]]}
DATA = {u'604': [[1361836800, {u'14885549': 52, u'91478624': 127, u'25581439': 12, u'532617990': 4}], [1361833200, {u'14885549': 38, u'91478624': 204, u'25581439': 14, u'40302362': 5, u'532617990': 2}]]}
我在这里尝试这样做(虽然确实很愚蠢,但是可行):
My attempt to do this is here (which is surely idiotic but works):
total = 0
for i in DATA:
for j in DATA[i]:
for k in j[1]:
total += j[1][k]
请帮助?
推荐答案
将 sum()
与生成器配合使用:
total = sum(sum(inner[1].values()) for outer in DATA.values() for inner in outer)
这在行为上等同于以下for循环:
This is equivalent in behavior to the following for loop:
total = 0
for outer in DATA.values():
for inner in outer:
total += sum(inner[1].values())
这篇关于列表聚合的python字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!