两个字典相加的方法
A = {'a': 1, 'b': 2, 'c': 3}
B = {'b': 4, 'c': 6, 'd': 8}
for key,value in B.items():
if key in A:
A[key] += value
else:
A[key] = value
dict(sorted(A.items(), key=lambda d:d[1]))
#结果:{'a': 1, 'b': 6, 'd': 8, 'c': 9}
来自:https://blog.csdn.net/weixin_30314793/article/details/102375770
|