简体   繁体   中英

Trying to bitwise and two lists in python

I did the following in the python shell. The first and third output is correct but the second and is just wrong. I know i can use the zip function to do this but i want to know why python does this.

>>> [1,1,1,1] and [1,0,0,0]
[1, 0, 0, 0]
>>> [1,0,0,0] and [1,1,0,0]
[1, 1, 0, 0]
>>> [1,1,1,1] and [0,0,0,0]
[0, 0, 0, 0]

As mentioned at : Python AND operator on two boolean lists - how?
"and simply returns either the first or the second operand, based on their truth value. If the first operand is considered false, it is returned, otherwise the other operand is returned." by Martijn Pieters

[1,1,1,1] and [1,0,0,0]
=> [1, 0, 0, 0] which is second operand while first is true.

Another example:

a=2
print(a==3 and [1,1,0,0])

return False while a==3 is false
And

a=2
print(a==2 and [1,1,0,0])

return [1,1,0,0] while a==2 is true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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