日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python炫技操作(02):合并字典的七種方法

系列導(dǎo)讀:Python 炫技操作(01):條件語句的七種寫法

創(chuàng)新互聯(lián)建站專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、南華網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為南華等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

Python 語言里有許多(而且是越來越多)的高級特性,是 Python 發(fā)燒友們非常喜歡的。在這些人的眼里,能夠?qū)懗瞿切┮话汩_發(fā)者看不懂的高級特性,就是高手,就是大神。

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

為什么這么說呢?我說下自己的看法:

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

該篇是「炫技系列」的第二篇內(nèi)容,在這個系列里,我將總結(jié)盤點一下,我所見過的那些炫技操作。在這里,如果你是 Python 發(fā)燒友,你可以學(xué)到一些寫出超酷的代碼書寫技巧。同時,看了這些內(nèi)容,對你在閱讀別人的代碼時,也許會有些幫助。

1. 最簡單的原地更新

字典對象內(nèi)置了一個 update 方法,用于把另一個字典更新到自己身上。

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> profile.update(ext_info)
  5. >>> print(profile)
  6. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

如果想使用 update 這種最簡單、最地道原生的方法,但又不想更新到自己身上,而是生成一個新的對象,那請使用深拷貝。

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> from copy import deepcopy
  5. >>>
  6. >>> full_profile = deepcopy(profile)
  7. >>> full_profile.update(ext_info)
  8. >>>
  9. >>> print(full_profile)
  10. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}
  11. >>> print(profile)
  12. {"name": "xiaoming", "age": 27}

2. 先解包再合并字典

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

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> full_profile01 = {**profile, **ext_info}
  5. >>> print(full_profile01)
  6. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}
  7. >>>
  8. >>> full_profile02 = dict(**profile, **ext_info)
  9. >>> print(full_profile02)
  10. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

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

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

3. 借助 itertools

在 Python 里有一個非常強大的內(nèi)置模塊,它專門用于操作可迭代對象。

正好我們字典也是可迭代對象,自然就可以想到,可以使用 itertools.chain() 函數(shù)先將多個字典(可迭代對象)串聯(lián)起來,組成一個更大的可迭代對象,然后再使用 dict 轉(zhuǎn)成字典。

 
 
 
 
  1. >>> import itertools
  2. >>>
  3. >>> profile = {"name": "xiaoming", "age": 27}
  4. >>> ext_info = {"gender": "male"}
  5. >>>
  6. >>>
  7. >>> dict(itertools.chain(profile.items(), ext_info.items()))
  8. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

4. 借助 ChainMap

如果可以引入一個輔助包,那我就再提一個, ChainMap 也可以達到和 itertools 同樣的效果。

 
 
 
 
  1. >>> from collections import ChainMap
  2. >>>
  3. >>> profile = {"name": "xiaoming", "age": 27}
  4. >>> ext_info = {"gender": "male"}
  5. >>>
  6. >>> dict(ChainMap(profile, ext_info))
  7. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

使用 ChainMap 有一點需要注意,當(dāng)字典間有重復(fù)的鍵時,只會取第一個值,排在后面的鍵值并不會更新掉前面的(使用 itertools 就不會有這個問題)。

 
 
 
 
  1. >>> from collections import ChainMap
  2. >>>
  3. >>> profile = {"name": "xiaoming", "age": 27}
  4. >>> ext_info={"age": 30}
  5. >>> dict(ChainMap(profile, ext_info))
  6. {'name': 'xiaoming', 'age': 27}

5. 使用dict.items() 合并

在 Python 3.9 之前,其實就已經(jīng)有 | 操作符了,只不過它通常用于對集合(set)取并集。

利用這一點,也可以將它用于字典的合并,只不過得繞個彎子,有點不好理解。

你得先利用 items 方法將 dict 轉(zhuǎn)成 dict_items,再對這兩個 dict_items 取并集,最后利用 dict 函數(shù),轉(zhuǎn)成字典。

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> full_profile = dict(profile.items() | ext_info.items())
  5. >>> full_profile
  6. {'gender': 'male', 'age': 27, 'name': 'xiaoming'}

當(dāng)然了,你如果嫌這樣太麻煩,也可以簡單點,直接使用 list 函數(shù)再合并(示例為 Python 3.x )

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> dict(list(profile.items()) + list(ext_info.items()))
  5. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

若你在 Python 2.x 下,可以直接省去 list 函數(shù)。

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> dict(profile.items() + ext_info.items())
  5. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

6. 最酷炫的字典解析式

Python 里對于生成列表、集合、字典,有一套非常 Pythonnic 的寫法。

那就是列表解析式,集合解析式和字典解析式,通常是 Python 發(fā)燒友的最愛,那么今天的主題:字典合并,字典解析式還能否勝任呢?

當(dāng)然可以,具體示例代碼如下:

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> {k:v for d in [profile, ext_info] for k,v in d.items()}
  5. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

7. Python 3.9 新特性

在 2 月份發(fā)布的 Python 3.9.04a 版本中,新增了一個抓眼球的新操作符操作符:|, PEP584 將它稱之為合并操作符(Union Operator),用它可以很直觀地合并多個字典。

 
 
 
 
  1. >>> profile = {"name": "xiaoming", "age": 27}
  2. >>> ext_info = {"gender": "male"}
  3. >>>
  4. >>> profile | ext_info
  5. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}
  6. >>>
  7. >>> ext_info | profile
  8. {'gender': 'male', 'name': 'xiaoming', 'age': 27}
  9. >>>
  10. >>>

除了 | 操作符之外,還有另外一個操作符 |=,類似于原地更新。

 
 
 
 
  1. >>> ext_info |= profile
  2. >>> ext_info
  3. {'gender': 'male', 'name': 'xiaoming', 'age': 27}
  4. >>>
  5. >>>
  6. >>> profile |= ext_info
  7. >>> profile
  8. {'name': 'xiaoming', 'age': 27, 'gender': 'male'}

看到這里,有沒有漲姿勢了,學(xué)了這么久的 Python ,沒想到合并字典還有這么多的方法。本篇文章的主旨,并不在于讓你全部掌握這 7 種合并字典的方法,實際上,你只要選用一種最順手的方式即可。

但是在協(xié)同工作中,或者在閱讀他人代碼時,你不可避免地會碰到各式各樣的寫法,這時候你能下意識的知道這是在做合并字典的操作,那這篇文章就是有意義的。


本文名稱:Python炫技操作(02):合并字典的七種方法
URL鏈接:http://www.5511xx.com/article/dhgpeho.html