Logical operation between two Boolean lists

前端 未结 7 834
终归单人心
终归单人心 2021-02-07 15:42

I get a weird result and I try to apply the and or the or operator to 2 Boolean lists in python. I actually get the exact opposite of what I was expec

7条回答
  •  渐次进展
    2021-02-07 16:12

    Both lists are truthy because they are non-empty.

    Both and and or return the operand that decided the operation's value.

    If the left side of and is truthy, then it must evaluate the right side, because it could be falsy, which would make the entire operation false (false and anything is false). Therefore, it returns the right side.

    If the left side of or is truthy, it does not need to evaluate the right side, because it already knows that the expression is true (true or anything is true). So it returns the left side.

    If you wish to perform pairwise comparisons of items in the list, use a list comprehension, e.g.:

    [x or y for (x, y) in zip(a, b)]     # a and b are your lists
    

提交回复
热议问题