我想在两个列表中得到OR的结果,例如
a = [0,0,1,1]
b = [0,1,0,1]
output = [0,1,1,1]
我尝试过output = a or b
,但答案不正确
答案 0 :(得分:1)
您可以将a
和b
映射到operator.or_
:
from operator import or_
output = map(or_, a, b)
output
变为:
[0, 1, 1, 1]
答案 1 :(得分:0)
output = map(lambda x, y: bool(x or y), a, b)
结果:
output == [False, True, True, True]
答案 2 :(得分:0)
请注意,此处可以使用or
或二进制运算符|
:
a = [0,0,1,1]
b = [0,1,0,1]
a_or_b = [aa | bb for aa, bb in zip(a, b)]
# this also works:
# a_or_b = [aa or bb for aa, bb in zip(a, b)]
print(a_or_b) # [0, 1, 1, 1]
答案 3 :(得分:0)
您可以使用'numpy'库并在相应轴上获取最大值:
import numpy as np
a = np.array([[0,0,1,1], [0,1,0,1]])
np.amax(a, axis=0)