简体   繁体   English

Python列表中的布尔值比较给出了奇怪的结果

[英]Python list of booleans comparison gives strange results

I try: 我尝试:

[True,True,False] and [True,True,True]

and get [True, True True] 得到[真,真如此]

but

[True,True,True] and [True,True,False]

gives

[True,True,False]

Not too sure why it's giving those strange results, even after taking a look at some other python boolean comparison questions. 不太确定为什么它会给出那些奇怪的结果,即使在看了一些其他的python布尔比较问题之后。 Integer does the same (replace True -> 1 and False ->0 above and the results are the same). 整数也是这样(替换True - > 1和False - > 0,结果相同)。 What am I missing? 我错过了什么? I obviously want 我显然想要

[True,True,False] and [True,True,True]

to evaluate to 评估

[True,True,False]

Any populated list evaluates to True . 任何填充列表的计算结果为True True and x produces x , the second list. True and x生成x ,第二个列表。

From the Python documentation : Python文档

The expression x and y first evaluates x; 表达式x和y首先计算x; if x is false, its value is returned; 如果x为false,则返回其值; otherwise, y is evaluated and the resulting value is returned. 否则,将评估y并返回结果值。

You're getting the second value returned. 你得到了第二个返回的值。

PS I had never seen this behavior before either, I had to look it up myself. PS我以前从未见过这种行为,我不得不自己查一下。 My naive expectation was that a boolean expression would yield a boolean result. 我天真的期望是布尔表达式会产生一个布尔结果。

Others have explained what's going on. 其他人已经解释了发生了什么。 Here are some ways to get what you want: 以下是获得所需内容的一些方法:

>>> a = [True, True, True]
>>> b = [True, True, False]

Use a listcomp: 使用listcomp:

>>> [ai and bi for ai,bi in zip(a,b)]
[True, True, False]

Use the and_ function with a map : and_函数与map一起使用:

>>> from operator import and_
>>> map(and_, a, b)
[True, True, False]

Or my preferred way (although this does require numpy ): 或者我喜欢的方式(虽然这确实需要numpy ):

>>> from numpy import array
>>> a = array([True, True, True])
>>> b = array([True, True, False])
>>> a & b
array([ True,  True, False], dtype=bool)
>>> a | b
array([ True,  True,  True], dtype=bool)
>>> a ^ b
array([False, False,  True], dtype=bool)

[True, True, False] is being evaluated as a boolean (because of the and operator), and evaluates to True since it is non-empty. [True, True, False]被评估为布尔值(因为and运算符),并且因为它是非空的,所以求值为True Same with [True, True, True] . [True, True, True] The result of either statement is then just whatever is after the and operator. 任何一个语句的结果就是and运算符之后的任何结果。

You could do something like [ai and bi for ai, bi in zip(a, b)] for lists a and b . 对于列表ab [ai and bi for ai, bi in zip(a, b)]您可以执行[ai and bi for ai, bi in zip(a, b)]的操作。

As far as I know, you need to zip through the list. 据我所知,你需要压缩列表。 Try a list comprehension of this sort: 尝试这种列表理解:

l1 = [True,True,False]
l2 = [True,True,True]
res = [ x and y for (x,y) in zip(l1, l2)]
print res

and returns the last element if they all are evaluated to True . and返回最后一个元素,如果它们都被评估为True

>>> 1 and 2 and 3
3

The same is valid for lists, which are evalueted to True if they are not empty (as in your case). 对于列表也是如此,如果它们不为空(如在您的情况下),则将其评估为True

Python works by short-circuiting its boolean and gives the result expression as the result. Python的工作原理是将其布尔值短路并将结果表达式作为结果。 A populated list evaluates to true and gives the result as the value of the second list. 填充列表的计算结果为true,并将结果作为第二个列表的值。 Look at this, when I just interchanged the position of your first and second list. 看看这个,当我刚刚交换你的第一个和第二个列表的位置时。

In [3]: [True,True,True] and [True, True, False]
Out[3]: [True, True, False]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM