Python AND operator on two boolean lists - how?

前端 未结 9 2152
感动是毒
感动是毒 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条回答
  •  没有蜡笔的小新
    2020-11-28 10:32

    In addition to what @Martijn Pieters has answered, I would just add the following code to explain and and or operations in action.

    and returns the first falsy value encountered else the last evaluated argument.

    Similarly or returns the first truthy value encountered else the last evaluated argument.

    nl1 = [3,3,3,3,0,0,0,0]
    nl2 = [2,2,0,0,2,2,0,0]
    nl3 = [1,0,1,0,1,0,1,0]
    and_list = [a and b and c for a,b,c in zip(nl1,nl2,nl3)]
    or_list = [a or b or c for a,b,c in zip(nl1,nl2,nl3)]
    

    Values are

    and_list = [1, 0, 0, 0, 0, 0, 0, 0]

    or_list = [3, 3, 3, 3, 2, 2, 1, 0]

提交回复
热议问题