[英]How to create single Python dict from a list of dicts by summing values with common keys?
I have a list of dictionaries, eg: 我有一个词典列表,例如:
dictList = [
{'a':3, 'b':9, 'c':4},
{'a':9, 'b':24, 'c':99},
{'a':10, 'b':23, 'c':88}
]
All the dictionaries have the same keys eg a , b , c . 所有词典都具有相同的键,例如a , b , c 。 I wish to create a single dictionary with the same keys where the values are the sums of the values with the same keys from all the dictionaries in the original list. 我希望创建一个具有相同键的单个字典,其中值是原始列表中所有字典中具有相同键的值的总和。
So for the above example, the output should be: 所以对于上面的例子,输出应该是:
{'a':22, 'b':56, 'c':191}
What would be the most efficient way of doing this? 这样做最有效的方法是什么? I currently have: 我目前有:
result = {}
for myDict in dictList:
for k in myDict:
result[k] = result.setdefault(k, 0) + myDict[k]
If all dicts have all keys, you could do this as: 如果所有的词都有所有键,你可以这样做:
>>> dict((key, sum(d[key] for d in dictList)) for key in dictList[0])
{'a': 22, 'b': 56, 'c': 191}
[Edit] If speed is a big priority, you can also shave off ~20% (though at the cost of some readability) with the following instead: [编辑]如果速度是一个重要的优先事项,你也可以用以下代码减少~20%(虽然以一些可读性为代价):
import operator, itertools
dict((key, sum(itertools.imap(operator.itemgetter(key), dictList)))
for key in dictList[0])
The speed depends on the size of the dict. 速度取决于字典的大小。 I get the following timings for the original 3 item list, and for various different sizes (created by mutliplying the original list by 10, 100 or 1000 etc): 我得到了原始3项列表的以下时间,以及各种不同的大小(通过将原始列表多出10,100或1000等创建):
List Size Original dict+generator imap+itemgetter
3 0.054 0.090 0.097
30 0.473 0.255 0.236
300 4.668 1.884 1.529
3000 46.668 17.975 14.499
(All times for 10,000 runs) (10,000次运行的所有时间)
So it's slightly slower for just 3, but two to three times as fast for larger lists. 所以它只有3个稍慢,但对于较大的列表来说要慢两到三倍。
Try this. 试试这个。
from collections import defaultdict
result = defaultdict(int)
for myDict in dictList:
for k in myDict:
result[k] += myDict[k]
I'm not sure how it relates to the other answers speed wise, but there is always 我不确定它与速度明智的其他答案有什么关系,但总有
from collections import Counter
result = sum(map(Counter,dictList),Counter())
Counter
is a subclass of dict
and it can be used in place of dict
in most places. Counter
是dict
的子类,它可以在大多数地方用来代替dict
。 If necessary, you could just convert it back into a dict
如有必要,您可以将其转换回dict
result = dict(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.