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中的字典合并和更新,了解一下

留言点赞关注

相关文章
相关标签/搜索