Python AND operator on two boolean lists - how?

前端 未结 9 2150
感动是毒
感动是毒 2020-11-28 09:36

I have two boolean lists, e.g.,

x=[True,True,False,False]
y=[True,False,True,False]

I want to AND these lists together, with the expected o

9条回答
  •  猫巷女王i
    2020-11-28 10:12

    This should do what you want:

    xy = [a and b for a, b in zip(x, y)]
    

    The reason x and y returns y and y and x returns x is because boolean operators in python return the last value checked that determines the true-ness of the expression. Non-empty list's evaluate to True, and since and requires both operands to evaluate True, the last operand checked is the second operand. Contrast with x or y, which would return x because it doesn't need to check y to determine the true-ness of the expression.

提交回复
热议问题