update()函数用于将两个字典合并操作,有相同的就覆盖
update()方法语法:
dict.update(dict2)
1.两个字典不存在相同的键
D = {'one': 1, 'two': 2}
D.update({'three': 3, 'four': 4}) # 传一个字典
print(D)
输出:
{‘one’: 1, ‘two’: 2, ‘three’: 3, ‘four’: 4}
2.两个字典存在相同的键-覆盖
D = {'one': 1, 'two': 2}
D.update({'two': 3, 'four': 4}) # 传一个字典,有重复的键
print(D)
输出:
{‘one’: 1, ‘two’: 3, ‘four’: 4}