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


当前回答

当与set一起使用时,它执行联合操作。

其他回答

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

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

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

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

当与set一起使用时,它执行联合操作。

这只是当前变量和另一个变量之间的OR运算。当T=True和F=False时,请以图形方式查看输出:

r s r|=s
T T T
T F T
F T T
F F F

例如:

>>> r=True
>>> r|=False
>>> r
True
>>> r=False
>>> r|=False
>>> r
False
>>> r|=True
>>> r
True

它表示按位或操作。

例子:

x = 5
x |= 3 #which is similar to x = x | 3
print(x)

答案:7

它是如何工作的?

The binary of 5 is : 0 1 0 1
The binary of 3 is : 0 0 1 1

OR operation : (If one of both sides are 1/True then result is 1/True)

0 1 0 1  #Binary of 5
0 0 1 1  #Binary of 3
---------------------
0 1 1 1  #Binary of 7

所以答案是7