1 A = {'a': 11, 'b': 22}
2 B = {'c': 48, 'd': 13}
3 #update() 把字典B的键/值对更新到A里
4 A.update(B)
5 print(A)
1 def sum_dict(a,b):
2 temp = dict()
3 # python3,dict_keys类似set; | 并集
4 for key in a.keys()| b.keys():
5 temp[key] = sum([d.get(key, 0) for d in (a, b)])
6 return temp
7
8 def test():
9 #python3使用reduce需要先导入
10 from functools import reduce
11 #[a,b,c]列表中的参数可以2个也可以多个,自己尝试。
12 return print(reduce(sum_dict,[a,b,c]))
13
14 a = {'a': 1, 'b': 2, 'c': 3}
15 b = {'a':1,'b':3,'d':4}
16 c = {'g':3,'f':5,'a':10}
17 test()
1 A = {'a': 11, 'b': 22}
2 B = {'a': 48, 'b': 13}
3 print({**A,**B})