输入两个字典dict1,dict2,返回两个字典的并集,如果两个字典相同的键对应不同的值,则把两个值构成一个元祖
def union_dict(dict1,dict2):
out_dict = dict1.copy()
for key2,value2 in dict2.items():
value1 = out_dict.setdefault(key2,value2)
if value1 != value2:
out_dict[key2] = (value1,value2)
return out_dict
dict1 = {'a':1,'b':2}
dict2 = {'a':1,'b':2}
print union_dict(dict1,dict2)
dict2 = {}
print union_dict(dict1,dict2)
dict2 = {'a':1,'b':3,'c':4}
print union_dict(dict1,dict2)