Python 炫技操作之合并字典的七種方法
Python 語言里有許多(而且是越來越多)的高級特性,是 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', 'age': 27}>>> 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’}
看到這里,有沒有漲姿勢了,學了這么久的 Python ,沒想到合并字典還有這么多的方法。本篇文章的主旨,并不在于讓你全部掌握這 7 種合并字典的方法,實際在工作中,你只要選用一種最順手的方式即可,但是在協同工作中,或者在閱讀他人代碼時,你不可避免地會碰到各式各樣的寫法,這時候你能下意識的知道這是在做合并字典的操作,那這篇文章就是有意義的。
總結
以上就是Python 炫技操作之合并字典的七種方法的詳細內容,更多關于python 合并字典的方法的資料請關注好吧啦網其它相關文章!
相關文章: