program tip

파이썬 목록의 모든 요소에 논리 연산자를 적용하는 방법

radiobox 2020. 11. 3. 07:55
반응형

파이썬 목록의 모든 요소에 논리 연산자를 적용하는 방법


파이썬에 부울 목록이 있습니다. 나는 그들을 AND (또는 OR 또는 NOT)하고 결과를 얻고 싶습니다. 다음 코드는 작동하지만 그다지 비단 해지지는 않습니다.

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

더 비단뱀처럼 만드는 방법에 대한 제안.


and모든 요소에 대해 논리적 입니다 a_list.

all(a_list)

or모든 요소에 대해 논리적 입니다 a_list.

any(a_list)

창의적이라고 생각되면 다음을 수행 할 수도 있습니다.

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)

내장은 ;-) 동안 그것들은 단락에서 평가되지 않는다는 것을 명심하십시오.

또 다른 재미있는 방법 :

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

그리고 또 다른 :

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

우리는 모든 일에 갈 수 있지만, 예, 파이썬 방법은 사용하는 것입니다 allany:-)

그건 그렇고, 파이썬에는 꼬리 재귀 제거 기능이 없으므로 LISP 코드를 직접 번역하지 마십시오 ;-)


ANDing 및 ORing은 쉽습니다.

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

주목하는 것도 매우 쉽습니다.

>>> [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.


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?"


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.


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)

참고URL : https://stackoverflow.com/questions/1790520/how-to-apply-a-logical-operator-to-all-elements-in-a-python-list

반응형