python合并两个字典的方法是什么

   2025-02-15 4470
核心提示:在Python中,可以使用update()方法来合并两个字典。示例代码如下:dict1 = {'a': 1, 'b': 2}dict2 = {'

在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中的对应值进行覆盖。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言