How do Python's any and all functions work?

后端 未结 8 1558
挽巷
挽巷 2020-11-22 01:13

I\'m trying to understand how the any() and all() Python built-in functions work.

I\'m trying to compare the tuples so that if any value i

相关标签:
8条回答
  • 2020-11-22 01:56

    You can roughly think of any and all as series of logical or and and operators, respectively.

    any

    any will return True when at least one of the elements is Truthy. Read about Truth Value Testing.

    all

    all will return True only when all the elements are Truthy.

    Truth table

    +-----------------------------------------+---------+---------+
    |                                         |   any   |   all   |
    +-----------------------------------------+---------+---------+
    | All Truthy values                       |  True   |  True   |
    +-----------------------------------------+---------+---------+
    | All Falsy values                        |  False  |  False  |
    +-----------------------------------------+---------+---------+
    | One Truthy value (all others are Falsy) |  True   |  False  |
    +-----------------------------------------+---------+---------+
    | One Falsy value (all others are Truthy) |  True   |  False  |
    +-----------------------------------------+---------+---------+
    | Empty Iterable                          |  False  |  True   |
    +-----------------------------------------+---------+---------+
    

    Note 1: The empty iterable case is explained in the official documentation, like this

    any

    Return True if any element of the iterable is true. If the iterable is empty, return False

    Since none of the elements are true, it returns False in this case.

    all

    Return True if all elements of the iterable are true (or if the iterable is empty).

    Since none of the elements are false, it returns True in this case.


    Note 2:

    Another important thing to know about any and all is, it will short-circuit the execution, the moment they know the result. The advantage is, entire iterable need not be consumed. For example,

    >>> multiples_of_6 = (not (i % 6) for i in range(1, 10))
    >>> any(multiples_of_6)
    True
    >>> list(multiples_of_6)
    [False, False, False]
    

    Here, (not (i % 6) for i in range(1, 10)) is a generator expression which returns True if the current number within 1 and 9 is a multiple of 6. any iterates the multiples_of_6 and when it meets 6, it finds a Truthy value, so it immediately returns True, and rest of the multiples_of_6 is not iterated. That is what we see when we print list(multiples_of_6), the result of 7, 8 and 9.

    This excellent thing is used very cleverly in this answer.


    With this basic understanding, if we look at your code, you do

    any(x) and not all(x)
    

    which makes sure that, atleast one of the values is Truthy but not all of them. That is why it is returning [False, False, False]. If you really wanted to check if both the numbers are not the same,

    print [x[0] != x[1] for x in zip(*d['Drd2'])]
    
    0 讨论(0)
  • 2020-11-22 02:06
    s = "eFdss"
    s = list(s)
    all(i.islower() for i in s )   # FALSE
    any(i.islower() for i in s )   # TRUE
    
    0 讨论(0)
提交回复
热议问题