And/Or python 中布尔值列表的元素
And/Or elemental-wise for list of bools in python
我在 python 中有两个布尔列表:
l1 = [True, False, True]
l2 = [False, True, True]
而且我希望对它们进行元素方面的比较 And/Or。我希望获得:
l3 = [False, False, True] # elemental-wise And
l4 = [True, True, True] # elemental-wise Or
所以我简单地说:
l3 = l1 and l2
l4 = l1 or l2
但结果却出乎意料:
l3=[False, True, True] (which is l2)
l4=[True, False, True] (which is l1)
我怎样才能干净整洁地完成元素方面的比较?
您可以使用列表理解
l3 = [(i and j) for i,j in zip(l1,l2)]
l4 = [(i or j) for i,j in zip(l1,l2)]
当你在做的时候
l3 = l1 and l2
l4 = l1 or l2
然后减少到
l3 = bool(l1) and l2
l4 = bool(l1) or l2
现在因为 l1
是非空列表,所以
bool(l1) = True
现在,我假设在 l3 = l1 and l2
的情况下,l1
被评估为 True
所以对于 short circuit evaluation
,它 returns l2
.
在 l4 = l1 or l2
的情况下,再次由于 short circuit evaluation
,l1
被 return 编辑,因为 l1
是 True
。
所以,你会得到这样的结果。
short circuit evaluation
只不过是--
- 在
A and B
的情况下,如果A评估为True,则继续评估B,return继续评估B的结果。如果A评估为False,则评估B没有意义。Return A. 的结果
False and print('hello')
>>> False
True and print('hello')
>>> hello
- 在
A or B
的情况下,如果 A 的计算结果为 True,则计算 B 没有意义。只需 return A 的结果。如果 A 的计算结果为 False,则 return B. 的结果
True or (1/0)
>>> True
False or (1/0)
>>> ZeroDivisionError: division by zero
注意:
bool([]) = False
bool([1,2,'a']) = True
bool(['False']) = True
您可以在列表理解中使用内置的 all()
and any()
方法:
l1 = [True, False, True]
l2 = [False, True, True]
l3 = [all(z) for z in zip(l1, l2)]
l4 = [any(z) for z in zip(l1, l2)]
print(l3)
print(l4)
输出
[False, False, True]
[True, True, True]
您也可以使用内置的map()
方法:
l1 = [True, False, True]
l2 = [False, True, True]
l3 = map(all, zip(l1, l2))
l4 = map(any, zip(l1, l2))
print(list(l3))
print(list(l4))
输出:
[False, False, True]
[True, True, True]
我在 python 中有两个布尔列表:
l1 = [True, False, True]
l2 = [False, True, True]
而且我希望对它们进行元素方面的比较 And/Or。我希望获得:
l3 = [False, False, True] # elemental-wise And
l4 = [True, True, True] # elemental-wise Or
所以我简单地说:
l3 = l1 and l2
l4 = l1 or l2
但结果却出乎意料:
l3=[False, True, True] (which is l2)
l4=[True, False, True] (which is l1)
我怎样才能干净整洁地完成元素方面的比较?
您可以使用列表理解
l3 = [(i and j) for i,j in zip(l1,l2)]
l4 = [(i or j) for i,j in zip(l1,l2)]
当你在做的时候
l3 = l1 and l2
l4 = l1 or l2
然后减少到
l3 = bool(l1) and l2
l4 = bool(l1) or l2
现在因为 l1
是非空列表,所以
bool(l1) = True
现在,我假设在 l3 = l1 and l2
的情况下,l1
被评估为 True
所以对于 short circuit evaluation
,它 returns l2
.
在 l4 = l1 or l2
的情况下,再次由于 short circuit evaluation
,l1
被 return 编辑,因为 l1
是 True
。
所以,你会得到这样的结果。
short circuit evaluation
只不过是--
- 在
A and B
的情况下,如果A评估为True,则继续评估B,return继续评估B的结果。如果A评估为False,则评估B没有意义。Return A. 的结果
False and print('hello')
>>> False
True and print('hello')
>>> hello
- 在
A or B
的情况下,如果 A 的计算结果为 True,则计算 B 没有意义。只需 return A 的结果。如果 A 的计算结果为 False,则 return B. 的结果
True or (1/0)
>>> True
False or (1/0)
>>> ZeroDivisionError: division by zero
注意:
bool([]) = False
bool([1,2,'a']) = True
bool(['False']) = True
您可以在列表理解中使用内置的 all()
and any()
方法:
l1 = [True, False, True]
l2 = [False, True, True]
l3 = [all(z) for z in zip(l1, l2)]
l4 = [any(z) for z in zip(l1, l2)]
print(l3)
print(l4)
输出
[False, False, True]
[True, True, True]
您也可以使用内置的map()
方法:
l1 = [True, False, True]
l2 = [False, True, True]
l3 = map(all, zip(l1, l2))
l4 = map(any, zip(l1, l2))
print(list(l3))
print(list(l4))
输出:
[False, False, True]
[True, True, True]