Sum corresponding elements of multiple python dict

2020-06-23 06:58发布

I have an arbitrary number of equal-length python dictionaries with matching sets of keys, like this:

{'a':1, 'b':4, 'c':8, 'd':9}

{'a':2, 'b':3, 'c':2, 'd':7}

{'a':0, 'b':1, 'c':3, 'd':4}
...

How can I obtain a single dictionary with the same set of keys but with values as the sums of corresponding elements in the dictionary set? In other words, I'd want:

{'a':3, 'b':8, 'c':13, 'd':20}

Maybe there's an ugly, complicated loop structure, but is there a nicer way to do this with some kind of list/dict comprehension cleverness? Come to think of it, I'm really not sure how to make an ugly loop version, anyway..

2条回答
欢心
2楼-- · 2020-06-23 07:21

If you simply want to use dict only, you can use this

dicts = [{'a':0, 'b':4, 'c':8, 'd':9},
         {'a':0, 'b':3, 'c':2, 'd':7},
         {'a':0, 'b':1, 'c':3, 'd':4}]

result = {}
for myDict in dicts:
    for key, value in myDict.items():
        result.setdefault(key, 0)
        result[key] += value
print result

Output:

{'a': 0, 'c': 13, 'b': 8, 'd': 20}
查看更多
够拽才男人
3楼-- · 2020-06-23 07:36

collections.Counter() to the rescue ;-)

from collections import Counter
dicts = [{'a':1, 'b':4, 'c':8, 'd':9},
         {'a':2, 'b':3, 'c':2, 'd':7},
         {'a':0, 'b':1, 'c':3, 'd':4}]
c = Counter()
for d in dicts:
    c.update(d)

Then:

>>> print c
Counter({'d': 20, 'c': 13, 'b': 8, 'a': 3})

Or you can change it back to a dict:

>>> print dict(c)
{'a': 3, 'c': 13, 'b': 8, 'd': 20}

It doesn't matter to Counter() whether all the input dicts have same keys. If you know for sure that they do, you could try ridiculous ;-) one-liners like this:

d = {k: v for k in dicts[0] for v in [sum(d[k] for d in dicts)]}

Counter() is clearer, faster, and more flexible. To be fair, though, this slightly less ridiculous one-liner is less ridiculous:

d = {k: sum(d[k] for d in dicts) for k in dicts[0]}
查看更多
登录 后发表回答