如果 L 中有 'a' 或 'b',其中 L 是一个列表(Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21344842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:37:53  来源:igfitidea点击:

if 'a' or 'b' in L, where L is a list (Python)

python

提问by Brian Leach

I am having trouble with the following logic:

我遇到以下逻辑问题:

Lets say I have a list L = ['a', 'b', 'c']

假设我有一个清单 L = ['a', 'b', 'c']



Both items are in the list...

两个项目都在列表中......

if ('a' or 'b') in L:
    print 'it\'s there!'
else:
    print 'No sorry'

prints It's there!

印刷 It's there!



Only the first item is in the list...

只有第一项在列表中......

if ('a' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'

prints It's there!

印刷 It's there!



Neither item in the list...

列表中的任何一项...

if ('e' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'

prints No sorry

印刷 No sorry



Here's the confusing oneOnly the seconditem in the list...

这是令人困惑的只有列表中的第二项......

if ('e' or 'a') in L:
    print 'it\'s there!'
else:
    print 'No sorry'

prints No sorry

印刷 No sorry



I do not understand why this is not registering as a true statement. How does this generalize to an orstatement with nconditionals?

我不明白为什么这不是真实的陈述。这如何推广到带有n 个条件的or语句?

Forehead-slapping easy answer in 3,2,1...

在 3,2,1 中拍额头的简单回答...

采纳答案by Steinar Lima

Let's break down the expression:

让我们分解一下表达式:

('e' or 'a')will first check if 'e'is True. If it is, the expression will return 'e'. If not, it will return 'a'.

('e' or 'a')将首先检查是否'e'为真。如果是,则表达式将返回'e'。如果没有,它将返回'a'

Since all non-empty strings returns True, this expression will always return 'e'. This means that if ('e' or 'a') in L:can be translated to if 'e' in L, which in this case is False.

由于所有非空字符串都返回True,因此该表达式将始终返回'e'。这意味着if ('e' or 'a') in L:可以转换为if 'e' in L,在本例中为False

A more generic way to check if a list contains at least one value of a set of values, is to use the anyfunction coupled with a generator expression.

检查列表是否包含一组值中的至少一个值的更通用方法是使用any与生成器表达式结合的函数。

if any(c in L for c in ('a', 'e')):

回答by aIKid

Use this instead:

改用这个:

 if 'a' in L or 'b' in L:

If we want to check if allthese of this "items" are in the list, alland a generator comprehension is your friend:

如果我们想检查所有这些“项目”是否都在列表中,all并且生成器理解是您的朋友:

items = 'a', 'b', 'c'
if all(i in L for i in items):

Or if anyof these items are in the list, use any:

或者,如果这些项目中有任何一项在列表中,请使用any

if any(i in L for i in items)

回答by Christian

Strings (except an empy string) will always evaluate to Truewhen they are evaluated as a boolean. While evaluating with or/andboth will return True, but there is a little difference between them:

字符串(除了 empy 字符串)True在它们被评估为布尔值时将始终评估为。虽然用or/and两者评估都会返回True,但它们之间有一点区别:

print 'a' or 'b'    # Output:  a
print 'a' and 'b'   # Output:  b

or: will return the first string and: will return the last string

or: 将返回第一个字符串 and: 将返回最后一个字符串

When you do

当你做

if ('a' or 'b') in L:

, it will check 'a' or 'b'which is 'a'and then check if 'a'is in L. Something similar happens with the other cases (based on what I explained before).

,它将检查'a' or 'b'哪个是'a',然后检查是否'a'L。其他情况也会发生类似的情况(基于我之前的解释)。

So when you do

所以当你做

if ('e' or 'a') in L:

, 'e' or 'a'will evaluate to 'e'and therefore it will print 'No Sorry', because 'e'is notin L.

'e' or 'a'将计算为'e',因此它会打印'No Sorry',因为'e'不是L

What you must do is compare whether elements are in the list separately:

你必须做什么比较元素是否在列表中分别

if 'a' in L or 'b' in L:
if 'a' in L or 'd' in L:
if 'e' in L or 'd' in L:
if 'e' in L or 'a' in L:

回答by Max Noel

The trick to the output you're getting is that andand orin Python always evaluate to one of their operands -- generally the one that had to be evaluated last to determine the truthiness of the operation:

诀窍你得到的输出是andor在Python总是为自己的一个操作数-通常是一个有最后被评估,以确定操作感实性:

1 or 2  # returns 1 because since 1 is true, there's no need to evaluate the second argument.
1 or 0  # returns 1, same thing.
0 or 2  # returns 2 because 0 is false, so we need to evaluate the second arg to check whether the operation is true.
0 or "" # returns "" (both 0 and "" are false).

1 and 2    # returns 2 because for an and operation to be true, both its operands need to be checked for truthiness.
0 and 2    # returns 0, because we know that if the first operand is false, so is the whole operation.
0 and None # Still returns 0, we don't even need to check the second operand.

So when you're evaluating (1 or 2) in [1, 3, 5](where in truth you want 1 in [1, 3, 5] or 2 in [1, 3, 5]), what really happens is (1 or 2)is evaluated to 1, and your operation becomes 1 in [1, 3, 5].

因此,当您进行评估时(1 or 2) in [1, 3, 5](实际上是您想要的地方1 in [1, 3, 5] or 2 in [1, 3, 5]),真正发生的(1 or 2)是评估为1,并且您的操作变为1 in [1, 3, 5]