python合并兩個(gè)字典的方法總結(jié)
1. 使用方法update()
通過(guò)使用Python中的update()方法,可以將一個(gè)列表合并到另一個(gè)列表中。但是在這種情況下,第二個(gè)列表被合并到第一個(gè)列表中,并且沒(méi)有創(chuàng)建新的列表。它返回None。
示例:
def merge(dict1, dict2): return(dict2.update(dict1)) # Driver code dict1 = {'a': 10, 'b': 8} dict2 = {'d': 6, 'c': 4} # This returns None print(merge(dict1, dict2)) # changes made in dict2 print(dict2)
輸出
None
{'c': 4, 'a': 10, 'b': 8, 'd': 6}
2. 使用 ** 操作符
這通常被認(rèn)為是Python中的一個(gè)技巧,其中使用單個(gè)表達(dá)式合并兩個(gè)字典并存儲(chǔ)在第三個(gè)字典中。使用 ** [星星]是一種快捷方式,它允許您直接使用字典將多個(gè)參數(shù)傳遞給函數(shù)。使用此方法,我們首先將第一個(gè)字典的所有元素傳遞到第三個(gè)字典,然后將第二個(gè)字典傳遞到第三個(gè)字典。這將替換第一個(gè)字典的重復(fù)鍵。
def merge(dict1, dict2): res = {**dict1, **dict2} return res # Driver code dict1 = {'a': 10, 'b': 8} dict2 = {'d': 6, 'c': 4} dict3 = merge(dict1, dict2) print(dict3)
輸出
{'a': 10, 'b': 8, 'd': 6, 'c': 4}
3. 使用 ‘|’ 運(yùn)算符 (Python 3.9)
在Python的3.9中,現(xiàn)在我們可以使用“|“運(yùn)算符來(lái)合并兩個(gè)字典。這是一種非常方便的字典合并方法。
def merge(dict1, dict2): res = dict1 | dict2 return res # Driver code dict1 = {'x': 10, 'y': 8} dict2 = {'a': 6, 'b': 4} dict3 = merge(dict1, dict2) print(dict3)
輸出
{'x': 10, 'a': 6, 'b': 4, 'y': 8}
4. 使用for循環(huán)和keys()方法
def merge(dict1, dict2): for i in dict2.keys(): dict1[i]=dict2[i] return dict1 # Driver code dict1 = {'x': 10, 'y': 8} dict2 = {'a': 6, 'b': 4} dict3 = merge(dict1, dict2) print(dict3)
輸出
{'x': 10, 'y': 8, 'a': 6, 'b': 4}
5. 使用ChainMap
在Python中合并字典的一種新方法是使用collections模塊中的內(nèi)置ChainMap類(lèi)。這個(gè)類(lèi)允許您創(chuàng)建多個(gè)字典的單個(gè)視圖,對(duì)ChainMap所做的任何更新或更改都將反映在底層字典中。
以下是如何使用ChainMap合并兩個(gè)字典的示例:
from collections import ChainMap # create the dictionaries to be merged dict1 = {'a': 1, 'b': 2} dict2 = {'c': 3, 'd': 4} # create a ChainMap with the dictionaries as elements merged_dict = ChainMap(dict1, dict2) # access and modify elements in the merged dictionary print(merged_dict['a']) # prints 1 print(merged_dict['c']) # prints 3 merged_dict['c'] = 5 # updates value in dict2 print(merged_dict['c']) # prints 5 # add a new key-value pair to the merged dictionary merged_dict['e'] = 6 # updates dict1 print(merged_dict['e']) # prints 6
輸出
1
3
5
6
使用ChainMap合并字典是一種簡(jiǎn)潔高效的方法,并且允許您輕松地更新和修改合并后的字典。
6. 使用dict構(gòu)造函數(shù)
def merge_dictionaries(dict1, dict2): merged_dict = dict1.copy() merged_dict.update(dict2) return merged_dict # Driver code dict1 = {'x': 10, 'y': 8} dict2 = {'a': 6, 'b': 4} print(merge_dictionaries(dict1, dict2))
輸出
{'x': 10, 'y': 8, 'a': 6, 'b': 4}
7. 使用dict構(gòu)造函數(shù)和union運(yùn)算符(|)
此方法使用dict()構(gòu)造函數(shù)和聯(lián)合運(yùn)算符(|)合并兩個(gè)字典。union運(yùn)算符組合兩個(gè)字典的鍵和值,并且兩個(gè)字典中的任何公共鍵從第二個(gè)字典中獲取值。
# method to merge two dictionaries using the dict() constructor with the union operator (|) def merge(dict1, dict2): # create a new dictionary by merging the items of the two dictionaries using the union operator (|) merged_dict = dict(dict1.items() | dict2.items()) # return the merged dictionary return merged_dict # Driver code dict1 = {'a': 10, 'b': 8} dict2 = {'d': 6, 'c': 4} # merge the two dictionaries using the Merge() function merged_dict = merge(dict1, dict2) # print the merged dictionary print(merged_dict)
輸出
{'d': 6, 'b': 8, 'c': 4, 'a': 10}
8. 使用reduce()方法
from functools import reduce def merge_dictionaries(dict1, dict2): merged_dict = dict1.copy() merged_dict.update(dict2) return merged_dict dict1 = {'a': 10, 'b': 8} dict2 = {'d': 6, 'c': 4} dict_list = [dict1, dict2] # Put the dictionaries into a list result_dict = reduce(merge_dictionaries, dict_list) print(result_dict)
輸出
{'a': 10, 'b': 8, 'd': 6, 'c': 4}
到此這篇關(guān)于python合并兩個(gè)字典的方法總結(jié)的文章就介紹到這了,更多相關(guān)python合并字典內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python實(shí)現(xiàn)字典合并的五種方法(附示例代碼)
- Python中的字典合并與列表合并技巧
- 詳解Python實(shí)現(xiàn)字典合并的四種方法
- 詳解Python 合并字典
- python 將列表里的字典元素合并為一個(gè)字典實(shí)例
- Python如何合并多個(gè)字典或映射
- python兩個(gè)_多個(gè)字典合并相加的實(shí)例代碼
- Python合并2個(gè)字典成1個(gè)新字典的方法(9種)
- python 實(shí)現(xiàn)二維字典的鍵值合并等函數(shù)
- Python編寫(xiě)合并字典并實(shí)現(xiàn)敏感目錄的小腳本
- Python實(shí)現(xiàn)合并兩個(gè)字典的8種方法
相關(guān)文章
Python創(chuàng)建二維數(shù)組實(shí)例(關(guān)于list的一個(gè)小坑)
下面小編就為大家?guī)?lái)一篇Python創(chuàng)建二維數(shù)組實(shí)例(關(guān)于list的一個(gè)小坑)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11如何解決pycharm中用matplotlib畫(huà)圖不顯示中文的問(wèn)題
這篇文章主要介紹了如何解決pycharm中用matplotlib畫(huà)圖不顯示中文的問(wèn)題,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06python+matplotlib實(shí)現(xiàn)鼠標(biāo)移動(dòng)三角形高亮及索引顯示
這篇文章主要介紹了Python+matplotlib實(shí)現(xiàn)鼠標(biāo)移動(dòng)三角形高亮及索引顯示,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01一文搞懂Python中pandas透視表pivot_table功能
透視表是一種可以對(duì)數(shù)據(jù)動(dòng)態(tài)排布并且分類(lèi)匯總的表格格式。或許大多數(shù)人都在Excel使用過(guò)數(shù)據(jù)透視表,也體會(huì)到它的強(qiáng)大功能,而在pandas中它被稱(chēng)作pivot_table,今天通過(guò)本文給大家介紹Python中pandas透視表pivot_table功能,感興趣的朋友一起看看吧2021-11-11膠水語(yǔ)言Python與C/C++的相互調(diào)用的實(shí)現(xiàn)
這篇文章主要介紹了膠水語(yǔ)言Python與C/C++的相互調(diào)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05python 裝飾器功能以及函數(shù)參數(shù)使用介紹
之前學(xué)習(xí)編程語(yǔ)言大多也就是學(xué)的很淺很淺,基本上也是很少涉及到裝飾器這些的類(lèi)似的內(nèi)容??偸怯X(jué)得是一樣很神奇的東西,舍不得學(xué)(嘿嘿)。今天看了一下書(shū)籍。發(fā)現(xiàn)道理還是很簡(jiǎn)單的2012-01-01Python在Windows和在Linux下調(diào)用動(dòng)態(tài)鏈接庫(kù)的教程
這篇文章主要介紹了Python在Windows和在Linux下調(diào)用動(dòng)態(tài)鏈接庫(kù)的教程,在進(jìn)行Python的CS端編程時(shí)經(jīng)常需要用到,需要的朋友可以參考下2015-08-08