Logical operation between two Boolean lists

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

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:09

    This is normal, because and and or actually evaluate to one of their operands. x and y is like

    def and(x, y):
        if x:
            return y
        return x
    

    while x or y is like

    def or(x, y):
        if x:
            return x
        return y
    

    Since both of your lists contain values, they are both "truthy" so and evaluates to the second operand, and or evaluates to the first.

提交回复
热议问题