Fastest way to merge n-dictionaries and add values on 2.6
本问题已经有最佳答案,请猛点这里访问。
我有一个字典列表,我想把它组合成一个字典,并在列表中添加每个字典的值。例如:
1 | ds = [{1: 1, 2: 0, 3: 0}, {1: 2, 2: 1, 3: 0}, {1: 3, 2: 2, 3: 1, 4: 5}] |
最后的结果应该是一个字典:
1 | merged = {1: 6, 2: 3, 3: 1, 4: 5} |
我对性能很感兴趣,正在寻找最快的实现,它可以将n个字典的列表合并到一个字典中并求和值。一个明显的实现是:
1 2 3 4 5 6 7 | from collections import defaultdict merged = defaultdict(int) for d in ds: for k, v in d.items(): merged[k] += v |
在python 2.6中有更快的方法吗?
到目前为止的一些时间安排:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | from random import randrange ds = [dict((randrange(1, 1000), randrange(1, 1000)) for i in xrange(500)) for i in xrange(10000)] # 10000 dictionaries of approx. length 500 from collections import defaultdict def merge1(dicts, defaultdict=defaultdict, int=int): merged = defaultdict(int) for d in dicts: for k in d: merged[k] += d[k] return merged def merge2(dicts): merged = {} merged_get = merged.get for d in dicts: for k in d: merged[k] = merged_get(k, 0) + d[k] return merged def merge3(dicts): merged = {} for d in dicts: for k in d: merged[k] = merged[k] + d[k] if k in merged else 0 return merged from timeit import timeit for func in ('merge1', 'merge2', 'merge3'): print func, timeit(stmt='{0}(ds)'.format(func), setup='from __main__ import merge1, merge2, merge3, ds', number=1) |
1 2 3 | merge1 0.992541510164 merge2 1.40478747997 merge3 1.23502204889 |