Python實現合并兩個字典的8種方法
在Python中,有多種方法可以通過使用各種函數和構造函數來合并字典。在本文中,我們將討論一些合并字典的方法。
1. 使用方法update()
通過使用Python中的update()方法,可以將一個列表合并到另一個列表中。但是在這種情況下,第二個列表被合并到第一個列表中,并且沒有創(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. 使用 ** 操作符
這通常被認為是Python中的一個技巧,其中使用單個表達式合并兩個字典并存儲在第三個字典中。使用 ** [星星]是一種快捷方式,它允許您直接使用字典將多個參數傳遞給函數。使用此方法,我們首先將第一個字典的所有元素傳遞到第三個字典,然后將第二個字典傳遞到第三個字典。這將替換第一個字典的重復鍵。
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. 使用 ‘|’ 運算符 (Python 3.9)
在Python的3.9中,現在我們可以使用“|“運算符來合并兩個字典。這是一種非常方便的字典合并方法。
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模塊中的內置ChainMap類。這個類允許您創(chuàng)建多個字典的單個視圖,對ChainMap所做的任何更新或更改都將反映在底層字典中。
以下是如何使用ChainMap合并兩個字典的示例:
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合并字典是一種簡潔高效的方法,并且允許您輕松地更新和修改合并后的字典。
6. 使用dict構造函數
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構造函數和union運算符(|)
此方法使用dict()構造函數和聯合運算符(|)合并兩個字典。union運算符組合兩個字典的鍵和值,并且兩個字典中的任何公共鍵從第二個字典中獲取值。
# 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}
到此這篇關于Python實現合并兩個字典的8種方法的文章就介紹到這了,更多相關Python 合并字典內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python 內置函數-range()+zip()+sorted()+map()+reduce()+filte
這篇文章主要介紹了python 內置函數-range()+zip()+sorted()+map()+reduce()+filter(),想具體了解函數具體用法的小伙伴可以參考一下下面的介紹,希望對你有所幫助2021-12-12Python實現將Markdown文檔轉為EPUB電子書文件
這篇文章主要為大家詳細介紹了Python如何實現將Markdown文檔轉為EPUB電子書文件,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-06-06selenium3+python3環(huán)境搭建教程圖解
這篇文章主要介紹了selenium3+python3環(huán)境搭建教程圖解,需要的朋友可以參考下2018-12-12Python 調用 ES、Solr、Phoenix的示例代碼
這篇文章主要介紹了Python 調用 ES、Solr、Phoenix的示例代碼,幫助大家更好的理解和學習python,感興趣的朋友可以了解下2020-11-11176萬!GPT-4發(fā)布詳解如何查看OpenAI的下載量
這篇文章主要為大家介紹了GPT-4發(fā)布如何查看OpenAI的下載量詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03