亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python實(shí)現(xiàn)合并兩個(gè)字典的8種方法

 更新時(shí)間:2024年07月23日 09:19:04   作者:程序員老冉  
Python有多種方法可以通過(guò)使用各種函數(shù)和構(gòu)造函數(shù)來(lái)合并字典,本文主要介紹了Python實(shí)現(xiàn)合并兩個(gè)字典的8種方法,具有一定的參考價(jià)值,感興趣的可以了解一下

在Python中,有多種方法可以通過(guò)使用各種函數(shù)和構(gòu)造函數(shù)來(lái)合并字典。在本文中,我們將討論一些合并字典的方法。

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實(shí)現(xiàn)合并兩個(gè)字典的8種方法的文章就介紹到這了,更多相關(guān)Python 合并字典內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論