Why Python built in “all” function returns True for empty iterables?

后端 未结 7 828
萌比男神i
萌比男神i 2020-12-05 02:08

I know it has a good reason, but I want to know what reason?

>>> print all([])
True

If all() is intended to check if every item on

相关标签:
7条回答
  • 2020-12-05 02:49

    This is expressed as "For all X in S, X is true". If S is empty, there are no X. However, the truth statement remains True, because for all X, X was true... there just aren't any X!

    Here is a explanation using logic.

    Consider two sets A and B where A+B is the union of the two sets.

    If any(A+B) = True -> any(A) or any(B) = True but we cannot assert either any(A)=True or any(B)=True.

    If any(A+B) = False -> any(A) = False and any(B) = False.

    If all(A+B) = True -> all(A)=True and all(B)=True

    if all(A+B) = False -> all(A)=False or all(B)=False but we cannot assert either all(A)=False or all(B)=False.

    Now instead of B, let's add the empty set 0 to A. We want to come up logic such that adding the empty set does not change the values of all() or any(), since A+0=A.

    any(A+0) = any(A) or any(0)

    any(0) must be False, so that if any(A) is True, any(A+0) is True, and if any(A) is False, any(A+0) is False.

    all(A+0) = all(A) and all(0)

    if all(A) is True, all(A+0) is True. Therefore, all(0) is True.

    0 讨论(0)
  • 2020-12-05 02:55

    Because all elements are True. When there are no elements, you can say that 'all elements are ... anything'

    0 讨论(0)
  • 2020-12-05 02:56

    all() (documented to "Return True if all elements of the iterable are true (or if the iterable is empty).") is equivalent to the following:

    def all(iterable):
        for element in iterable:
            if not element:
                return False
        return True
    

    Since there are no elements, it will skip the loop and return True.

    0 讨论(0)
  • 2020-12-05 02:56

    This comes from the mathematical logic.

    "everything is true of the elements of the empty set" (http://en.wikipedia.org/wiki/Empty_set)

    See also http://en.wikipedia.org/wiki/Vacuous_truth

    0 讨论(0)
  • 2020-12-05 02:58

    ELI5 version.

    Take a list of numbers

    L = [1,2,3,4,6]
    
    all([isintance(l, int) for l in L])
    

    all is defined in such a way that, the only way to make it False is by supplying at least one non-integer.

    Similarly any is defined in a way that, to make it True all you need is at-least one positive integer.

    Since all() is the complement of any() one must be True and other must be False

    0 讨论(0)
  • 2020-12-05 03:00

    When testing for a condition, we want the first element to always be added to the list. For example, if we only want to add numbers to a list if they are less than the smallest number or greater than the largest number we can do this:

    def addToList(less,num):
        if less:
            if any( num >= savedNum for savedNum in numbers):
                print('{} is not less'.format(num))
                return
        elif any( num <= savedNum for savedNum in numbers):
            print('{} is not greater'.format(num))
            return
    
        numbers.append(num)
    
    
    numbers = []
    doLess = True
    doMore = False
    addToList(doLess,5) #numbers is empty, but will be added
    addToList(doLess,2)
    addToList(doLess,7)
    addToList(doMore,15)
    addToList(doMore,9)
    print(numbers)
    

    Output:

    7 is not less [5, 2]
    9 is not greater [5, 2, 15]
    [5, 2, 15]
    
    0 讨论(0)
提交回复
热议问题