import os,sys
import copy
from collections import Counter
x = {"a":1,"b":2}
y = {"c":2,"d":4}
# method_1
z = dict(x,**y)
print z
# method_2
z = copy.deepcopy(x)
z.update(y)
print z
# method_3
z = Counter()
k = list(set(x.keys() + y.keys()))
for key in k:
z[key] += x.get(key,0)
z[key] += y.get(key,0)
print z
# method_4
z = Counter()
d = (x.items() + y.items())
for k,v in d:
z[k] += v
print z
# method_5
z = Counter()
for k,v in x.items():
z[k] += v
for k,v in y.items():
z[k] += v
print z
python trick合并字典小技巧整理
最新推荐文章于 2021-11-13 14:41:04 发布