【經驗分享】Python經典操作:合併字典的七種方法

Python 語言裏有許多(而且是越來越多)的高級特性,是 Python 發燒友們非常喜歡的。在這些人的眼裏,能夠寫出那些一般開發者看不懂的高級特性,就是高手,就是大神。

但你要知道,在團隊合作裏,炫技是大忌。

爲什麼這麼說呢?我說下自己的看法:

  1. 越簡潔的代碼,越清晰的邏輯,就越不容易出錯;
  2. 在團隊合作中,你的代碼不只有你在維護,降低別人的閱讀/理解代碼邏輯的成本是一個良好的品德
  3. 簡單的代碼,只會用到最基本的語法糖,複雜的高級特性,會有更多的依賴(如語言的版本)

該篇是「炫技系列」的第二篇內容,在這個系列裏,我將總結盤點一下,我所見過的那些炫技操作。在這裏,如果你是 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 或者 {} 就可以合併。

在學習過程中有什麼不懂得可以加我的
python學習qun,855408893
羣裏有不錯的學習視頻教程、開發工具與電子書籍。  
與你分享python企業當下人才需求及怎麼從零基礎學習好python,和學習什麼內容

>>> 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 )

在學習過程中有什麼不懂得可以加我的
python學習qun,855408893
羣裏有不錯的學習視頻教程、開發工具與電子書籍。  
與你分享python企業當下人才需求及怎麼從零基礎學習好python,和學習什麼內容

>>> 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學習扣QUN:⑧⑤⑤-④零⑧-⑧⑨③從零基礎開始到Python各領域的項目實戰教程、開發工具與電子書籍。與你分享企業當下對於python人才需求及學好python的高效技巧,不停更新最新教程!點擊加入我們的 python學習圈

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章