Featured image of post Python 3.9.0 小知识 - PEP584:增加字典合并操作符

Python 3.9.0 小知识 - PEP584:增加字典合并操作符

PEP584:增加字典合并操作符

内置字典类增加合并( | )与更新( |= )操作符

就是增加了合并操作符和更新操作符,简单应用下

1
2
3
4
5
6
7

>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
>>> e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> d | e
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> e | d
{'aardvark': 'Ethel', 'spam': 1, 'eggs': 2, 'cheese': 3}
1
2
3
4

>>> d |= e
>>> d
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}

文章参考:链接1

Licensed under CC BY-NC-SA 4.0
最后更新于 Jul 30, 2025 11:43 +0800