How to apply a logical operator to all elements in a python list

后端 未结 6 1669
日久生厌
日久生厌 2021-01-29 22:25

I have a list of booleans in python. I want to AND (or OR or NOT) them and get the result. The following code works but is not very pythonic.

def apply_and(alist         


        
6条回答
  •  孤街浪徒
    2021-01-29 22:45

    Here's another solution:

    def my_and(a_list):
        return not (False in a_list)
    
    def my_or(a_list):
        return True in a_list
    

    ANDing all elements will return True if all elements are True, hence no False in a list. ORing is similar, but it should return True if at least one True value is present in a list.

提交回复
热议问题