python实现:字典的合并(相同key的value相加)及字典的输出排序(各种意义下)_python字典合并与排序_Roxannekkk的博客-CSDN博客
dict1={'a':2,'b':3} dict2={'a':3,'b':2}
dict3={'c':3,'d':7}
合并
key相同,后一个字典覆盖前一个字典的value;key不同,新增
dict1.update(dict2) print(dict1) dict1.update(dict3) print(dict1) {'a': 3, 'b': 2} {'a': 3, 'b': 2, 'c': 3, 'd': 7}
相加
key相同,value相加;key不同,新增
from collections import Counter print(dict(Counter(dict1) + Counter(dict2))) print(dict(Counter(dict1) + Counter(dict3))) {'a': 5, 'b': 5} {'a': 2, 'b': 3, 'c': 3, 'd': 7}
标签:dict1,Python,相加,Counter,key,print,字典 From: https://www.cnblogs.com/daizichuan/p/17679729.html