所有vs和AND任何vs或

时间:2014-03-19 15:12:23

标签: python

我很想知道

之间有什么区别
all and "and"
any and "or"

例如: STATUS1 = 100,STATUS2 = 300,STATUS3 =​​ 400

哪个更好用:

if status1==100 and status2 ==300 and status3 ==400:

if all([status1==100,status2==300,status3=400]):

同样适用于任何和/或条件:

   if status1==100 or status2 ==300 or status3==400:
or
   if any([status1==100, status2 ==300, status3==400])

使用内置函数或原语或和条件哪一个更有效?

2 个答案:

答案 0 :(得分:16)

关键字andor遵循Python的short circuit evaluation规则。由于allany是函数,因此将评估所有参数。如果某些条件是函数调用,则可能会得到不同的行为。

答案 1 :(得分:-1)

<强> TL;博士

据我所知,all最好在你可能比较不同数量的布尔语句时使用and对于有限布尔语句更好,并且当使用{{1}时尝试使用生成器函数。

详细说明

编辑(为了明确使用短期电话一词) 它们在有限语句中的使用是首选,因为一旦可以确定真值,all将使每个布尔语句的评估短路。 请参阅答案的结尾以获取证据和详细示例。

由于任何包含连续Python语句的语句将是and,如果至少有一个语句是False,那么编译器只知道检查到达到一个错误答案:

False

它会检查status1 == 100 and status2 == 300 and status3 == 400 是否发现status1 == 100,它会立即停止处理该语句,如果它是False,如果现在检查True等等

这种逻辑可以使用循环直观地演示:

我们正在编写和语句的行为的图像,你会检查整行的每个语句并确定它们是否都是status2 == 300并返回True或者我们会找到True } value并返回False。您可以在到达第一个虚假陈述后节省时间,然后立即退出。

False

并且对于def and(statements): for statement in statements: if not statement: return False return True ,我们会编写逻辑,一旦找到or语句就会退出,因为这证明所有或语句与整个语句的整体真实性无关:

True

def or(statements): for statement in statements: if statement: return True return False and陈述混合在一起时,这种逻辑当然是混合的,并且适当地遵循操作的顺序

orand语句用于避免这种情况:

any

collection_of_numbers = [100,200,300,400,500,600,.....] if collection_of_numbers[0] == 100 and collection_of_numbers[1] == 200 and .......: print "All these numbers make up a linear set with slope 100" else: print "There was a break in the pattern!!!"

类似
or

例如:

collection_of_numbers = [100,200,300,400,500,600,.....]
if collection_of_numbers[0] == 100 or collection_of_numbers[1] == 200 or .......:
    print "One of these numbers was a multiple of 100"
else:
    print "None of these numbers were multiples of 100"

一种愚蠢的例子,但我认为它展示了temp = [] itr = 0 for i in collection_of_numbers: temp.append(i == itr) itr += 100 if all(temp): print "The numbers in our collection represent a linear set with slope 100" else: print "The numbers in out collection do not represent a linear set with slope 100" 可能有用的场景类型。

类似的论点适用于任何:

all

虽然可以说你将使用循环来节省更多时间来实现这种逻辑。

代替temp = [] for i in collection_of_numbers: temp.append(i%3 == 0) if any(temp): print "There was at least one number in our collect that was divisible by three" else: print "There were no numbers in our collection divisible by three"

and

不同之处在于,在检查每一个条目之前,这将会中断,这样可以在大型套装中节省大量时间,因为早期条目会破坏您的条件。

代替all

itr = 0
result = True
for i in collection_of_numbers:
    if not i == itr:
        result = False
        break
    itr += 100
if result:
    print "The numbers in our collection represent a linear set with slope 100"
else:
    print "The numbers in out collection do not represent a linear set with slope 100"

这将检查,直到找到一个满足条件为止,之后不会改变语句的or

**编辑**以上使用短路措辞和声明证明的例子。 考虑

any

temp = []
result = False
for i in collection_of_numbers:
    if i%3 == 0:
        result = True
        break
if result:
    print "There was at least one number in our collect that was divisible by three"
else:
    print "There were no numbers in our collection divisible by three"

第一个语句会将True评估为1 == 2 and 2 == 2 ,整个语句会立即短路并被评估为all([1 == 2, 2 == 2]) 。第二个语句将1 == 2评估为FalseFalse评估为1 == 2,然后在输入函数False后,它将返回2 == 2 }。必须首先评估每个语句的额外步骤是,为什么在检查一些小型有限的布尔检查集不使用该函数时更好。

虽然两个语句无关紧要,但如果你举一个极端的例子,你会看到我所说的所有布尔语句的评估是短路的。以下测试以不同的方式评估True布尔语句并计算其执行时间。每个语句的第一个布尔语句都会在整个布尔语句中导致短路,而不是评估

<强> test.py

and

跑步时:

False

对于陈述的短路评估的影响在这里由一个过高的因素清楚地表明。您可以看到,对于任何类型的有限布尔语句,即使是最好的方法也是使用显式语句,正如我在冗长答案的开头所述。这些函数适用于您可能不知道需要评估多少布尔语句的情况。