首发于微信公众号:《Python编程时光》python
Python 语言里有许多(并且是愈来愈多)的高级特性,是 Python 发烧友们很是喜欢的。在这些人的眼里,可以写出那些通常开发者看不懂的高级特性,就是高手,就是大神。编程
但你要知道,在团队合做里,炫技是大忌。微信
为何这么说呢?我说下本身的见解:ide
该篇是「炫技系列」的第二篇内容,在这个系列里,我将总结盘点一下,我所见过的那些炫技操做。在这里,若是你是 Python 发烧友,你能够学到一些写出超酷的代码书写技巧。同时,看了这些内容,对你在阅读别人的代码时,也许会有些帮助。函数
字典对象内置了一个 update 方法,用于把另外一个字典更新到本身身上。code
>>> 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}
使用 **
能够解包字典,解包完后再使用 dict 或者 {}
就能够合并。blog
>>> 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'}
在 Python 里有一个很是强大的内置模块,它专门用于操做可迭代对象。it
正好咱们字典也是可迭代对象,天然就能够想到,可使用 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'}
若是能够引入一个辅助包,那我就再提一个, 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}
在 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", "age": 27} >>> ext_info = {"gender": "male"} >>> >>> dict(profile.items() + ext_info.items()) {'name': 'xiaoming', 'age': 27, 'gender': 'male'}
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'}
在 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'}
看到这里,有没有涨姿式了,学了这么久的 Python ,没想到合并字典还有这么多的方法。本篇文章的主旨,并不在于让你所有掌握这 7 种合并字典的方法,实际在工做中,你只要选用一种最顺手的方式便可,可是在协同工做中,或者在阅读他人代码时,你不可避免地会碰到各式各样的写法,这时候你能下意识的知道这是在作合并字典的操做,那这篇文章就是有意义的。