搜索
您的当前位置:首页正文

python合并字典的七种方法

来源:意榕旅游网
python合并字典的七种⽅法

1. 最简单的原地更新

字典对象内置了⼀个 update ⽅法,⽤于把另⼀个字典更新到⾃⼰⾝上。

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>

>>> profile.update(ext_info)>>> print(profile)

{'name': 'xiaoming', 'age': 27, 'gender': 'male'}

如果想使⽤ update 这种最简单、最地道原⽣的⽅法,但⼜不想更新到⾃⼰⾝上,⽽是⽣成⼀个新的对象,那请使⽤深拷贝。

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>

>>> from copy import deepcopy>>>

>>> full_profile = deepcopy(profile)>>> full_profile.update(ext_info)>>>

>>> print(full_profile)

{'name': 'xiaoming', 'age': 27, 'gender': 'male'}>>> print(profile)

{\"name\": \"xiaoming\", \"age\": 27}

2. 先解包再合并字典

使⽤ ** 可以解包字典,解包完后再使⽤ dict 或者 {} 就可以合并。

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>

>>> full_profile01 = {**profile, **ext_info}>>> print(full_profile01)

{'name': 'xiaoming', 'age': 27, 'gender': 'male'}>>>

>>> full_profile02 = dict(**profile, **ext_info)>>> print(full_profile02)

{'name': 'xiaoming', 'age': 27, 'gender': 'male'}

若你不知道 dict(**profile, **ext_info) 做了啥,你可以将它等价于

>>> dict(((\"name\", \"xiaoming\"), (\"age\", 27), (\"gender\", \"male\"))){'name': 'xiaoming', 'age': 27, 'gender': 'male'}

3. 借助 itertools

在 Python ⾥有⼀个⾮常强⼤的内置模块,它专门⽤于操作可迭代对象。

正好我们字典也是可迭代对象,⾃然就可以想到,可以使⽤ itertools.chain() 函数先将多个字典(可迭代对象)串联起来,组成⼀个更⼤的可迭代对象,然后再使⽤ dict 转成字典。

>>> import itertools>>>

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>>>>

>>> dict(itertools.chain(profile.items(), ext_info.items())){'name': 'xiaoming', 'age': 27, 'gender': 'male'}

4. 借助 ChainMap

如果可以引⼊⼀个辅助包,那我就再提⼀个, ChainMap 也可以达到和 itertools 同样的效果。

>>> from collections import ChainMap>>>

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}

>>>

>>> dict(ChainMap(profile, ext_info))

{'name': 'xiaoming', 'age': 27, 'gender': 'male'}

使⽤ ChainMap 有⼀点需要注意,当字典间有重复的键时,只会取第⼀个值,排在后⾯的键值并不会更新掉前⾯的(使⽤ itertools 就不会有这个问题)。

>>> from collections import ChainMap>>>

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info={\"age\": 30}

>>> dict(ChainMap(profile, ext_info)){'name': 'xiaoming', 'age': 27}

5. 使⽤dict.items() 合并

在 Python 3.9 之前,其实就已经有 | 操作符了,只不过它通常⽤于对集合(set)取并集。利⽤这⼀点,也可以将它⽤于字典的合并,只不过得绕个弯⼦,有点不好理解。

你得先利⽤ items ⽅法将 dict 转成 dict_items,再对这两个 dict_items 取并集,最后利⽤ dict 函数,转成字典。

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>

>>> full_profile = dict(profile.items() | ext_info.items())>>> full_profile

{'gender': 'male', 'age': 27, 'name': 'xiaoming'}

当然了,你如果嫌这样太⿇烦,也可以简单点,直接使⽤ list 函数再合并(⽰例为 Python 3.x )

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>

>>> dict(list(profile.items()) + list(ext_info.items())){'name': 'xiaoming', 'age': 27, 'gender': 'male'}

若你在 Python 2.x 下,可以直接省去 list 函数。

>>> profile = {\"name\": \"xiaoming\>>> ext_info = {\"gender\": \"male\>>>

>>> dict(profile.items() + ext_info.items()){'name': 'xiaoming', 'age': 27, 'gender': 'male'}

6. 最酷炫的字典解析式

Python ⾥对于⽣成列表、集合、字典,有⼀套⾮常 Pythonnic 的写法。

那就是列表解析式,集合解析式和字典解析式,通常是 Python 发烧友的最爱,那么今天的主题:字典合并,字典解析式还能否胜任呢?当然可以,具体⽰例代码如下:

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>

>>> {k:v for d in [profile, ext_info] for k,v in d.items()}{'name': 'xiaoming', 'age': 27, 'gender': 'male'}

7. Python 3.9 新特性

在 2 ⽉份发布的 Python 3.9.04a 版本中,新增了⼀个抓眼球的新操作符操作符: |, PEP584 将它称之为合并操作符(Union Operator),⽤它可以很直观地合并多个字典。

>>> profile = {\"name\": \"xiaoming\", \"age\": 27}>>> ext_info = {\"gender\": \"male\"}>>>

>>> profile | ext_info

{'name': 'xiaoming', 'age': 27, 'gender': 'male'}>>>

>>> ext_info | profile

{'gender': 'male', 'name': 'xiaoming', 'age': 27}>>>>>>

除了 | 操作符之外,还有另外⼀个操作符 |=,类似于原地更新。

>>> ext_info |= profile>>> ext_info

{'gender': 'male', 'name': 'xiaoming', 'age': 27}>>>>>>

>>> profile |= ext_info>>> profile

{'name': 'xiaoming', 'age': 27, 'gender': 'male'}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top