Python3.9字典合并(|)和更新(| =)运算符

Python3.9字典合并(|)和更新(| =)运算符

python3.9新减少了兼并 (|) 与革新 (|=) 演算符,介入内置的 dict 类。

它们为现有的 dict.update 和 {**d1, **d2} 字典兼并本领供给了弥补。

>>> x = {"key1": "value1 from x", "key2": "value2 from x"}

>>> y = {"key2": "value2 from y", "key3": "value3 from y"}

>>> x | y

{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}

>>> y | x

{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}

分享到 :

Leave a Reply

Your email address will not be published. Required fields are marked *