Python 程序将两个字典组合成一个字典
当需要将两个字典连接成一个实体时,可以使用“update”方法。
字典是“键值”对。
下面演示了这一方法 −
示例
my_dict_1 = {'J':12,'W':22}
my_dict_2 = {'M':67}
print("The first dictionary is :")
print(my_dict_1)
print("The second dictionary is :")
print(my_dict_2)
my_dict_1.update(my_dict_2)
print("The concatenated dictionary is :")
print(my_dict_1)输出
The first dictionary is :
{'J': 12, 'W': 22}
The second dictionary is :
{'M': 67}
The concatenated dictionary is :
{'J': 12, 'W': 22, 'M': 67}说明
定义了两个字典,并显示在控制台上。
- 在第一个字典中调用“update”方法,同时将第二个字典作为参数传递。
- 这将有助于连接字典。
- 它显示在控制台上。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP