阅读量:105
在Python中,可以使用update()方法来合并两个字典。
示例代码如下:
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
dict1.update(dict2)
print(dict1)
输出结果为:
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
update()方法将字典dict2中的键值对添加到字典dict1中,如果有相同的键,则会用dict2中的对应值进行覆盖。