谷歌不让我搜索|=,所以我很难找到相关的文件。有人知道吗?


当前回答

|=在对象对之间执行原地+操作。特别是,在:

Sets:并集操作 Dicts:更新操作 计数器:(多集的)并集操作 数字:按位或,二进制操作

在大多数情况下,它与|操作符相关。请看下面的例子。

Sets

例如,赋值给s1和s2的两个集合的并集共享以下等价表达式:

>>> s1 = s1 | s2                                           # 1
>>> s1 |= s2                                               # 2
>>> s1.__ior__(s2)                                         # 3

其中s1的最终值等价于:

指定的或运算 就地或就地操作 通过特殊方法++的就地或操作

例子

这里我们将OR(|)和in-place OR(|=)应用于集合:

>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}

>>> # OR, | 
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1                                                     # `s1` is unchanged
{'a', 'b', 'c'}

>>> # In-place OR, |=
>>> s1 |= s2
>>> s1                                                     # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}

字典

在Python 3.9+中,字典之间提出了新的合并(|)和更新(|=)操作符。注意:这些操作符与上面提到的集合操作符不同。

给定赋值给d1和d2的两个字典之间的运算:

>>> d1 = d1 | d2                                           # 1
>>> d1 |= d2                                               # 2

其中d1通过:

指定的合并权限操作 就地合并-权利(更新)操作;相当于d1.update(d2)

例子

这里我们对dicts应用merge(|)和update (|=):

>>> d1 = {"a": 0, "b": 1, "c": 2}
>>> d2 = {"c": 20, "d": 30}

>>> # Merge, | 
>>> d1 | d2
{"a": 0, "b": 1, "c": 20, "d": 30}
>>> d1 
{"a": 0, "b": 1, "c": 2}

>>> # Update, |=
>>> d1 |= d2
>>> d1 
{"a": 0, "b": 1, "c": 20, "d": 30}

计数器

的集合。计数器与称为多集(mset)的数学数据结构有关。它基本上是(对象,多重性)键-值对字典。

给定分配给c1和c2的两个计数器之间的操作:

>>> c1 = c1 | c2                                           # 1
>>> c1 |= c2                                               # 2

其中c1是等效的via:

指定的联合操作 现场工会操作

多集的联合包含每个条目的最大多重度。注意,这与两个集合之间或两个常规字典之间的行为不同。

例子

在这里,我们将union(|)和in-place union(|=)应用于Counters:

import collections as ct


>>> c1 = ct.Counter({2: 2, 3: 3})
>>> c2 = ct.Counter({1: 1, 3: 5})

>>> # Union, |    
>>> c1 | c2
Counter({2: 2, 3: 5, 1: 1})
>>> c1
Counter({2: 2, 3: 3})

>>> # In-place Union, |=
>>> c1 |= c2
>>> c1
Counter({2: 2, 3: 5, 1: 1})

数字

最后,你可以做二进制数学。

给定n1和n2两个数字之间的运算:

>>> n1 = n1 | n2                                           # 1
>>> n1 |= n2                                               # 2

其中n1是通过等价的:

指定的位或操作 就地按位或操作

例子

这里我们对数字应用按位或(|)和就地按位或(|=):

>>> n1 = 0
>>> n2 = 1

>>> # Bitwise OR, |
>>> n1 | n2
1
>>> n1
0

>>> # In-place Bitwise OR, |=
>>> n1 |= n2
>>> n1
1

审查

本节简要回顾一些位数学。在最简单的情况下,按位OR操作比较两个二进制位。它总是返回1,除非两位都是0。

>>> assert 1 == (1 | 1) == (1 | 0) == (0 | 1)
>>> assert 0 == (0 | 0)

现在我们将这个概念扩展到二进制数之外。给定任意两个整数(没有分数分量),我们应用位或运算,得到一个积分结果:

>>> a = 10 
>>> b = 16 
>>> a | b
26

如何?通常,按位操作遵循一些“规则”:

内部比较二进制等价物 应用操作 返回作为给定类型的结果

让我们把这些规则应用到上面的正则整数上。

(1)比较二进制等价,这里是字符串(0b表示二进制):

>>> bin(a)
'0b1010'
>>> bin(b)
'0b10000'

(2)对每一列应用按位或操作(当两列都为0时为0,否则为1):

01010
10000
-----
11010

(3)返回给定类型的结果,例如以10为基数,十进制:

>>> int(0b11010)
26

内部二进制比较意味着我们可以将后者应用于任何进制的整数,例如十六进制和八进制:

>>> a = 10                                   # 10, dec
>>> b = 0b10000                              # 16, bin
>>> c = 0xa                                  # 10, hex
>>> d = 0o20                                 # 16, oct

>>> a | b
26
>>> c | d
26

另请参阅

An example of overloading the __ior__() method to iterate iterables in a MutableSet abstract base class R. Hettinger's OrderedSet recipe (see lines 3 and 10 respectively) A thread on Python-ideas on why to use |= to update a set A section B.8 of Dive in Python 3 on special methods of Python operators In-place binary operators fallback to regular methods, see cpython source code (eval.c and abstract.c). Thanks @asottile. A post on how python handles displaying prepended zeros in bitwise computations

+就地按位OR操作符不能应用于字面量;为对象分配名称。

特殊方法返回与其对应操作符相同的操作。

其他回答

给出一个用例(在花时间分析其他答案之后):

def process(item):
   return bool(item) # imagine some sort of complex processing taking place above

def any_success(data): # return True if at least one is successful
    at_least_one = False
    for item in data:
       at_least_one |= process(item)
    return at_least_one

>>> any_success([False, False, False])
False
>>> any_success([True, False, False])
True
>>> any_success([False, True, False])
True

基本上没有短路:如果你需要处理每个项目并记录至少一次成功,可能会有用。

请参见此回答中的注意事项

希望这也能帮助其他人理解:

dict1 = {'a': 'dict1', 'b': 'dict1', 'c': 'dict1'}
dict2 = {'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}

dict3 = dict1.copy()
dict3 = dict3 | dict2
dict4 = dict1.copy()
dict4 |= dict2
print(f'dict1:\n {dict1}')
print(f'dict2:\n {dict2}')
print(f'dict1 after dict1 = dict1 | dict2 (dict2 index c replaces dict1 index c, items in dict1 are discarded if present in dict2):\n {dict3}')
print(f'dict1 after dict1 |= dict2 (same behaviour as dict1 = dict1 | dict2):\n {dict4}')

dict5 = dict1.copy()
dict5 = dict2 | dict5
dict6 = dict2.copy()
dict6 |= dict1
print(f'dict1 after dict1 = dict2 | dict1 (dict2 index c is missing, dict1 index c was retained, items in dict2 are discarded if present in dict1):\n {dict5}')
print(f'dict2 after dict2 |= dict1 (same behaviour as dict2 = dict2 | dict1):\n {dict6}')


dict1:
 {'a': 'dict1', 'b': 'dict1', 'c': 'dict1'}
dict2:
 {'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}
dict1 after dict1 = dict1 | dict2 (dict2 index c replaces dict1 index c, items in dict1 are discarded if present in dict2):
 {'a': 'dict1', 'b': 'dict1', 'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}
dict1 after dict1 |= dict2 (same behaviour as dict1 = dict1 | dict2):
 {'a': 'dict1', 'b': 'dict1', 'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}
dict1 after dict1 = dict2 | dict1 (dict2 index c is missing, dict1 index c was retained, items in dict2 are discarded if present in dict1):
 {'c': 'dict1', 'd': 'dict2', 'e': 'dict2', 'a': 'dict1', 'b': 'dict1'}
dict2 after dict2 |= dict1 (same behaviour as dict2 = dict2 | dict1):
 {'c': 'dict1', 'd': 'dict2', 'e': 'dict2', 'a': 'dict1', 'b': 'dict1'}

它是位或。 假设我们有32 | 10,图片32和10的二进制:

32 = 10 0000
10 = 00 1010

现在,因为|是“或”运算,对这两个数字进行“或”运算

即1或0——> 1 0或0——> 0。沿着这个链条继续下去:

10 0000 | 00 1010 = 10 1010.

现在把二进制变成十进制,10 1010 = 42。

对于|=,考虑已知的例子,x +=5。这意味着x = x + 5,因此如果我们有x |= 5,这意味着x = x位或5。

|是位或。所以x |= y等价于x = x | y。

对于集合,|有一个相关的含义:集合并集。与你在数学中使用OR求两个集合的交集相同,你可以在python中使用|

*注意:这两个表达式不是100%等效的。在x |= y后,id(x) == id(y)。x = x | y后,id(x) != id(y)

在Python中,|=(ior)类似于联合运算。 例如,如果x=5和x|=5,那么两个值都将首先转换为二进制值,然后执行并集操作,我们得到答案5。