alw*_*btc 64 python pipe bitwise-operators
我|在函数调用中看到一个"管道"字符():
res = c1.create(go, come, swim, "", startTime, endTime, "OK", ax|bx)
Run Code Online (Sandbox Code Playgroud)
管道的含义是什么ax|bx?
gui*_*ume 107
这也是联合集合运算符
set([1,2]) | set([2,3])
Run Code Online (Sandbox Code Playgroud)
这将导致 set([1, 2, 3])
Ahm*_*gle 56
它是整数的按位OR.例如,如果一个或两个ax或bx是1,这个计算结果为1,否则到0.这也适用于其他的整数,例如15 | 128 = 143,即00001111 | 10000000 = 10001111二进制.
小智 35
在Python 3.9 - PEP 584 - Add Union Operators To dict标题为规范的部分中,对运算符进行了解释。管道被增强以合并(联合)字典。
>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
>>> e = {'cheese': 4, 'nut': 5}
>>> d | e
{'spam': 1, 'eggs': 2, 'cheese': 4, 'nut': 5} # comment 1
>>> e | d
{'cheese': 3, 'nut': 5, 'spam': 1, 'eggs': 2} # comment 2
Run Code Online (Sandbox Code Playgroud)
评论 1如果一个键同时出现在两个操作数中,则最后看到的值(即来自右侧操作数的值)获胜 --> 'cheese': 4 而不是 'cheese': 3
comment 2奶酪出现两次,因此选择第二个值d[cheese]=3
Tag*_*gar 10
是的,上面的所有答案都是正确的.
虽然你可以找到更多的"|"异常用例,但是如果它是一个类使用的重载运算符,例如,
https://github.com/twitter/pycascading/wiki#pycascading
input = flow.source(Hfs(TextLine(), 'input_file.txt'))
output = flow.sink(Hfs(TextDelimited(), 'output_folder'))
input | map_replace(split_words, 'word') | group_by('word', native.count()) | output
Run Code Online (Sandbox Code Playgroud)
在这个特定的用例管道"|" 运算符可以更好地被认为是unix管道运算符.但我同意,逐位运算符和联合集运算符是更常见的"|"用例 在Python中.