python combined dictionary, the same key value by adding

1. Take two dictionaries and sets focus on the same key value is added.

from collections import Counter

x = { 'apple': 1, 'banana': 2 }
y = { 'banana': 10, 'pear': 11 }

X,Y = Counter(x), Counter(y)
z = dict(X+Y)
[out] z = {'apple': 1, 'banana': 12, 'pear': 11}

 2. References

Published 38 original articles · 98 won praise · views 360 000 +

Guess you like

Origin blog.csdn.net/xijuezhu8128/article/details/88555254