Python中字典(dict)合并的几种方法

mac2024-04-03  42

Python中字典(dict)合并的几种方法

文章目录

Python中字典(dict)合并的几种方法一、字典的update()方法二、字典的dict(d1, **d2)和(**d1,**d2)方法1.dict(d1, **d2)2.dict(**d1,**d2) 三、字典的常规处理方法 输出结果均为:

{'name': 'coulson', 'age': '25', 'ins': 'hkx303', 'company': 'redhat'}

一、字典的update()方法

common = {'name': 'coulson', 'age': '25'} social = {'ins': 'hkx303', 'company': 'redhat'} info = {} info.update(common) info.update(social) print(info)

二、字典的dict(d1, **d2)和(**d1,**d2)方法

1.dict(d1, **d2)

common = {'name': 'coulson', 'age': '25'} social = {'ins': 'hkx303', 'company': 'redhat'} info = dict(common, **social) print(info)

2.dict(**d1,**d2)

common = {'name': 'coulson', 'age': '25'} social = {'ins': 'hkx303', 'company': 'redhat'} info = dict(**common, **social) print(info)

三、字典的常规处理方法

common = {'name': 'coulson', 'age': '25'} social = {'ins': 'hkx303', 'company': 'redhat'} info = dict(**common, **social) for key, value in common.items(): info[key] = value for key, value in social.items(): info[key] = value print(info)
最新回复(0)