Python3 3.9中的字典合併和更新,理解一下

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.htmlhtml

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。程序員

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。antd

咱們廢話少說,下面來點乾貨纔是正事。async

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。函數

如下是一些常見用法:3d

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。code

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。htm

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。blog

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。遊戲

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.html

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。

咱們廢話少說,下面來點乾貨纔是正事。

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。

如下是一些常見用法:

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.html

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。

咱們廢話少說,下面來點乾貨纔是正事。

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。

如下是一些常見用法:

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.html

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。

咱們廢話少說,下面來點乾貨纔是正事。

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。

如下是一些常見用法:

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.html

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。

咱們廢話少說,下面來點乾貨纔是正事。

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。

如下是一些常見用法:

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.html

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。

咱們廢話少說,下面來點乾貨纔是正事。

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。

如下是一些常見用法:

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.html

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。

咱們廢話少說,下面來點乾貨纔是正事。

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。

如下是一些常見用法:

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

感謝做者分享-http://bjbsair.com/2020-04-07/tech-info/30744.html

Python3.9中的字典合併和更新,瞭解一下

Python3.9正在積極開發,並計劃於今年10月發佈。

2月26日,開發團隊發佈了alpha 4版本。該版本引入了新的合併(|)和更新(|=)運算符,這個新特性幾乎影響了全部Python程序員。

咱們廢話少說,下面來點乾貨纔是正事。

字典

字典,一般稱爲dict,是Python中最重要的內置數據類型之一。這種數據類型是大小靈活的鍵值對集合,而且因爲它哈希實現,它以具備恆定的數據查找時間而聞名。

如下是一些常見用法:

# Declare a dict  
student = {'name': 'John', 'age': 14}# Get a value  
age = student['age']  
# age is 14# Update a value  
student['age'] = 15  
# student becomes {'name': 'John', 'age': 15}# Insert a key-value pair  
student['score'] = 'A'  
# student becomes {'name': 'John', 'age': 15, 'score': 'A'}

合併字典——舊方法

有時,兩個字典須要被合併來作進一步的處理。在3.9版本正式發佈以前,有幾種方法能夠作到這一點。假設有兩個dict:d1和d2。咱們想要建立一個新的dict:d3,它是d1和d2的集合。若是合併的dict之間有一些重疊的鍵,爲了說明應該作什麼,引入另外一個dict,d2a,它有一個與d1重疊的鍵。

# two dicts to start with  
d1 = {'a': 1, 'b': 2}  
d2 = {'c': 3, 'd': 4}  
d2a = {'a': 10, 'c': 3, 'd': 4}# target dict  
d3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

使用update() 方法

Python3.9中的字典合併和更新,瞭解一下

第一種方法是使用dict的方法update()。下面的代碼片斷展現瞭如何作到這一點。請注意,必須首先建立一個d1的副本,由於update() 函數將修改原始的dict。

# create a copy of d1, as update()modifies the dict in-place  
d3 = d1.copy()  
# d3 is {'a': 1, 'b': 2}# update the d3 with d2  
d3.update(d2)  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}

當有重疊的鍵時,必須更加謹慎地選擇保留哪些值。正如在下面看到的,在update() 方法中做爲參數傳遞的dict將經過重疊鍵(例如‘a’)的值(如10)來「贏得」遊戲。

d3 = d1.copy()  
d3.update(d2a)  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# This is not the way that we wantd3 = d2a.copy()  
d3.update(d1)  
# d3 now is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# This is the way that we want

打開字典

第二種方法是使用字典的打開。與上述方法相似,當有重疊的鍵時,「最後出現」的獲勝。

# unpacking  
d3 = {**d1, **d2}  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not rightd3 = {**d2a, **d1}  
# d3 is {'a': 1, 'c': 3, 'd': 4, 'b': 2}  
# Good

使用Dict(iterable, **kwarg)

在Python中建立字典的一種方法是使用 dict(iterable, **kwarg)類函數。與當前主題特別相關的是,當iterable是一個dict,將使用相同的鍵值對建立一個新的dict。至於關鍵字參數,能夠傳遞另外一個dict,這樣它將會將鍵值對添加到將要建立的dict中。請注意,這個關鍵字參數dict將用相同的鍵替換該值,相似於「最後出現」的獲勝。請看下面的例子。

d3 = dict(d1, **d2)  
# d3 is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# Good, it's what we wantd3 = dict(d1, **d2a)  
# d3 is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# Not right, 'a' value got replaced

須要注意的是,只有當關鍵字參數dict以字符串做爲關鍵字時,該方法纔有效。以下所示,使用 int 做爲關鍵字的dict是行不通的。

>>> dict({'a': 1}, **{2:3})  
Traceback (most recent call last):  
 File "<stdin>", line 1,in <module>  
TypeError: keywords must be strings  
>>> dict({'a': 1}, **{'2': 3})  
{'a': 1, '2': 3}

合併字典——新功能

Python3.9中的字典合併和更新,瞭解一下

在最新發布的Python 3.9.0a4中,能夠很是方便地使用合併運算符|來合併兩個dict。下面給出了一個例子。你可能已經注意到,當這兩個dict之間有重疊的鍵時,最後出現的會留下,這種行爲與上面看到的一致,好比update() 方法。

# use the merging operator |  
d3 = d1 | d2  
# d3 is now {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 = d1 | d2a  
# d3 is now {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

與這個合併操做符相關的是在環境中操做的參數賦值版本(例如更新左側的dict)。本質上,它的功能與update()方法相同。下面的代碼片斷展現了它的用法:

# Create a copy for d1  
d3 = d1.copy()# Use the augmented assignment of the merge operator  
d3 |= d2  
# d3 now is {'a': 1, 'b': 2, 'c': 3, 'd': 4}  
# goodd3 |= d2a  
# d3 now is {'a': 10, 'b': 2, 'c': 3, 'd': 4}  
# not good

在今天的文章裏,咱們回顧了Python3.9中合併和更新字典的新特性。在幾個模塊中還有新的更新和改進,例如asyncio, math和os模塊。

期待當它正式發佈時,能發現更多驚喜,你準備好了嗎?

Python3.9中的字典合併和更新,瞭解一下

留言點贊關注

相關文章
相關標籤/搜索