如何对 python 列表中的所有元素应用逻辑运算符

我有一个 Python 中布尔值的列表。我想与(或或或不)他们,并得到结果。下面的代码可以工作,但不是很 Python 化。

def apply_and(alist):
if len(alist) > 1:
return alist[0] and apply_and(alist[1:])
else:
return alist[0]

任何关于如何使它更加蟒蛇欣赏的建议。

113549 次浏览

Logical and across all elements in a_list:

all(a_list)

Logical or across all elements in a_list:

any(a_list)

If you feel creative, you can also do:

import operator
def my_all(a_list):
return reduce(operator.and_, a_list, True)


def my_any(a_list):
return reduce(operator.or_, a_list, False)

keep in mind that those aren't evaluated in short circuit, whilst the built-ins are ;-)

another funny way:

def my_all_v2(a_list):
return len(filter(None,a_list)) == len(a_list)


def my_any_v2(a_list):
return len(filter(None,a_list)) > 0

and yet another:

def my_all_v3(a_list):
for i in a_list:
if not i:
return False
return True


def my_any_v3(a_list):
for i in a_list:
if i:
return True
return False

and we could go on all day, but yes, the pythonic way is to use all and any :-)

By the way, Python has not tail recursion elimination, so don't try to translate LISP code directly ;-)

Reduce can do this:

reduce(lambda a,b: a and b, alist, True)

As fortran mentioned, all is the most succinct way to do it. But reduce answers the more general question "How to apply a logical operator to all elements in a python list?"

ANDing and ORing is easy:

>>> some_list = [True] * 100
# OR
>>> any(some_list)
True
#AND
>>> all(some_list)
True
>>> some_list[0] = False
>>> any(some_list)
True
>>> all(some_list)
False

NOTing is also fairly easy:

>>> [not x for x in some_list]
[True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False]

Of course, how you would use those results might require some interesting applications of DeMorgan's theorem.

As the other answers show, there are multiple ways to accomplish this task. Here's another solution that uses functions from the standard library:

from functools import partial


apply_and = all
apply_or = any
apply_not = partial(map, lambda x: not x)


if __name__ == "__main__":
ls = [True, True, False, True, False, True]
print "Original: ", ls
print "and: ", apply_and(ls)
print "or: ", apply_or(ls)
print "not: ", apply_not(ls)

The idiom for such operations is to use the reduce function (global in Python 2.X, in module functools in Python 3.X) with an appropriate binary operator either taken from the operator module or coded explicitly. In your case, it's operator.and_

reduce(operator.and_, [True, True, False])

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.