from functools import reduce
d1 = {
'charm': 1,
'ccc': 2,
'hp': 3
}
d2 = {
'charm': 2,
'hp': 4
}
arr = [d1, d2]
def sumDict(v1, v2):
allKeys = set(list(v1.keys()) + list(v2.keys())) #取所有key的聯集
result = dict()
for k in allKeys: #對所有key做壘加
result[k] = v1.get(k, 0) + v2.get(k, 0) #給預設值
return result
print(reduce(sumDict, arr))
ZnJvbSBmdW5jdG9vbHMgaW1wb3J0IHJlZHVjZQoKZDEgPSB7CgknY2hhcm0nOiAxLAoJJ2NjYyc6IDIsCgknaHAnOiAzCn0KCmQyID0gewoJJ2NoYXJtJzogMiwKCSdocCc6IDQKfQoKYXJyID0gW2QxLCBkMl0KCmRlZiBzdW1EaWN0KHYxLCB2Mik6CglhbGxLZXlzID0gc2V0KGxpc3QodjEua2V5cygpKSArIGxpc3QodjIua2V5cygpKSkgICAj5Y+W5omA5pyJa2V555qE6IGv6ZuGCglyZXN1bHQgPSBkaWN0KCkKCWZvciBrIGluIGFsbEtleXM6ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICPlsI3miYDmnIlrZXnlgZrlo5jliqAKCQlyZXN1bHRba10gPSB2MS5nZXQoaywgMCkgKyB2Mi5nZXQoaywgMCkgICAgICAgICPntabpoJDoqK3lgLwKCQkKCXJldHVybiByZXN1bHQKCQpwcmludChyZWR1Y2Uoc3VtRGljdCwgYXJyKSk=