“全部"原因和“任何"结果在空清单上 [英] Reason for "all" and "any" result on empty lists

查看:55
本文介绍了“全部"原因和“任何"结果在空清单上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,内置函数 all any 分别针对空的可迭代对象返回TrueFalse.我意识到,如果不是这样,仍然可以提出这个问题.但是我想知道为什么选择了该特定行为.是任意的,即.是不是很容易就变成了另一种方式?还是有潜在的原因?

In Python, the built-in functions all and any return True and False respectively for empty iterables. I realise that if it were the other way around, this question could still be asked. But I'd like to know why that specific behaviour was chosen. Was it arbitrary, ie. could it just as easily have been the other way, or is there an underlying reason?

(我问的原因仅仅是因为我不记得哪个是哪个,如果我知道其背后的理由,那么我可能会发现.还有,好奇心.)

(The reason I ask is simply because I never remember which is which, and if I knew the rationale behind it then I might. Also, curiosity.)

推荐答案

一些类比...

您有一个袜子抽屉,但当前为空.它是否包含任何黑色袜子?不-您根本没有袜子,所以您当然没有黑色的袜子.显然,any([])必须返回false-如果返回true,则违反直觉.

You have a sock drawer, but it is currently empty. Does it contain any black sock? No - you don't have any socks at all so you certainly don't have a black one. Clearly any([]) must return false - if it returned true this would be counter-intuitive.

all([])的情况稍微困难一些.请参阅有关虚假事实的Wikipedia文章.另一个比喻:如果一个房间里没有人,那么这个房间里的每个人都可以说法语.

The case for all([]) is slightly more difficult. See the Wikipedia article on vacuous truth. Another analogy: If there are no people in a room then everyone in that room can speak French.

数学上可以写成all([]):

其中集合A为空.

where the set A is empty.

关于是否应该将虚假陈述视为真实存在很多争论,但是从逻辑的角度来看,这是最有意义的:

There is considerable debate about whether vacuous statements should be considered true or not, but from a logical viewpoint it makes the most sense:

所有虚无的true陈述都是正确的主要论据如下:如逻辑条件的文章中所述,命题逻辑的公理要求如果P为假,则P => Q为真.也就是说,如果我们接受这些公理,就必须接受虚无的真实陈述确实是真实的.

The main argument that all vacuously true statements are true is as follows: As explained in the article on logical conditionals, the axioms of propositional logic entail that if P is false, then P => Q is true. That is, if we accept those axioms, we must accept that vacuously true statements are indeed true.

也来自文章:

似乎没有直接的理由去选择真实的东西.只是如果我们不这样做,事情就会在我们的脸上炸毁.

There seems to be no direct reason to pick true; it’s just that things blow up in our face if we don’t.

在Python中定义虚假为真"的语句以返回false会违反最小惊讶原则.

Defining a "vacuously true" statement to return false in Python would violate the principle of least astonishment.

这篇关于“全部"原因和“任何"结果在空清单上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆