36

我知道它有一个很好的理由,但我想知道是什么原因?

>>> print all([])
True

如果 all() 旨在检查 iterable 上的每个项目是否评估为“True”,并且我们知道空列表被评估为 False

>>> bool([])
False

那么为什么 all() 为空列表返回 True 呢?

<编辑>

我已经阅读了文档,并且我知道实现

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

但问题是为什么不呢?

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

这有逻辑吗?如果您有已完成任务的列表

today_todo_status = [task.status for task in my_todo if task.date == today]
can_i_go_home = all(today_todo_status)

好的,在上面的假设示例中,如果我没有任务,那么我就可以回家了。

但是还有其他情况,我不认为 all() 是为待办事项列表制作的。哈哈

</编辑>

4

7 回答 7

37

这表示为“对于 S 中的所有 X,X 为真”。如果 S 为空,则没有 X。然而,真值陈述仍然是 True,因为对于所有 X,X 都是真的……只是没有任何 X!

这是使用逻辑的解释。

考虑两个集合 A 和 B,其中 A+B 是这两个集合的并集。

如果 any(A+B) = True -> any(A) 或 any(B) = True 但我们不能断言 any(A)=True 或 any(B)=True。

如果 any(A+B) = False -> any(A) = False 并且 any(B) = False。

如果 all(A+B) = True -> all(A)=True 并且 all(B)=True

如果 all(A+B) = False -> all(A)=False 或 all(B)=False 但我们不能断言 all(A)=False 或 all(B)=False。

现在代替 B,让我们将空集 Ø 添加到 A。我们想要提出这样的逻辑,即添加空集不会改变 all() 或 any() 的值,因为 A+Ø=A。

任意(A+Ø)=任意(A)或任意(Ø)

any(Ø) 必须为 False,因此如果 any(A) 为 True,则 any(A+Ø) 为 True,如果 any(A) 为 False,则 any(A+Ø) 为 False。

全部(A+Ø)=全部(A)和全部(Ø)

如果 all(A) 为 True,则 all(A+Ø) 为 True。因此,all(Ø) 为真。

于 2012-08-16T01:57:16.247 回答
10

all()(记录为“如果可迭代对象的所有元素都为真(或可迭代对象为空),则返回 True。 ”)等价于以下内容:

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

由于没有元素,它将跳过循环并返回True

于 2012-08-16T01:56:50.697 回答
2

假设all([])False

然后,对于所有非空列表Aall(A + [])也应False

all(A + []) = all(A) and all([])
            = all(A) and False
            = False

因为A + [] = A,我们知道

all(A + []) = all(A) for any non empty list A

但是,all(A)可能是True(例如,A = [True]

因此,

对于所有非空列表Aall(A + [])也应该是False

这自相矛盾。结果,第一个假设是错误的,并且

all([])True

于 2020-09-23T16:55:36.747 回答
1

因为所有元素都是 True。当没有元素时,你可以说'所有元素都是......任何东西'

于 2012-08-16T01:56:58.247 回答
1

这来自数理逻辑。

“空集的元素一切都是真的”(http://en.wikipedia.org/wiki/Empty_set

另见http://en.wikipedia.org/wiki/Vacuous_truth

于 2012-08-16T02:24:00.793 回答
0

ELI5 版本。

获取数字列表

L = [1,2,3,4,6]

all([isintance(l, int) for l in L])

all以这样一种方式定义,唯一的方法False是提供at least一个非整数。

类似any的定义方式是,要使它成为True你所需要的只是at-least一个正整数。

因为是一个必须是而另一个必须all()是的补码any()TrueFalse

于 2018-12-04T13:09:41.567 回答
0

在测试条件时,我们希望始终将第一个元素添加到列表中。例如,如果我们只想将小于最小数字或大于最大数字的数字添加到列表中,我们可以这样做:

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)

输出:

7 is not less [5, 2]
9 is not greater [5, 2, 15]
[5, 2, 15]
于 2019-10-28T01:03:45.090 回答