2个列表的布尔运算

时间:2018-11-15 20:37:38

标签: python python-2.7

我想在两个列表中得到OR的结果,例如

a = [0,0,1,1]
b = [0,1,0,1]
output = [0,1,1,1]

我尝试过output = a or b,但答案不正确

4 个答案:

答案 0 :(得分:1)

您可以将ab映射到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)