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

如果';a';或';b';在L中,L是一个列表(Python),python,Python,我对以下逻辑有问题: 假设我有一个列表L=['a','b','c'] 这两项都在列表中 if ('a' or 'b') in L: print 'it\'s there!' else: print 'No sorry' if ('a' or 'd') in L: print 'it\'s there!' else: print 'No sorry' if ('e' or 'd') in L: print 'it\'s there!' else:

我对以下逻辑有问题:

假设我有一个列表
L=['a','b','c']


这两项都在列表中

if ('a' or 'b') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
if ('a' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
if ('e' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
if ('e' or 'a') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
打印
它就在那里


列表中只有第一项

if ('a' or 'b') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
if ('a' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
if ('e' or 'd') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
if ('e' or 'a') in L:
    print 'it\'s there!'
else:
    print 'No sorry'
打印
它就在那里


列表中的两项都不是

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


这里是令人困惑的一个只有列表中的第二项

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


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

用3,2,1…,简单回答拍打额头,用这个代替:

 if 'a' in L or 'b' in L:
如果我们想检查这些“项目”是否都在列表中,
all
和生成器理解是您的朋友:

items = 'a', 'b', 'c'
if all(i in L for i in items):
或者,如果列表中有任何项目,请使用
any

if any(i in L for i in items)

让我们将表达式分解为:

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

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

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

if any(c in L for c in ('a', 'e')):
字符串(empy字符串除外)在作为布尔值计算时,其计算结果始终为
True
。使用
或/和
进行计算时,两者都将返回
True
,但它们之间有一点区别:

print 'a' or 'b'    # Output:  a
print 'a' and 'b'   # Output:  b
:将返回第一个字符串
:将返回最后一个字符串

当你这样做的时候

if ('a' or 'b') in L:
if ('e' or 'a') in L:
,它将检查
'a'或'b'
哪个是
'a'
,然后检查
'a'
是否在
L
中。类似的情况也发生在其他案例中(根据我之前的解释)

所以当你这么做的时候

if ('a' or 'b') in L:
if ('e' or 'a') in L:
'e'或'a'
将计算为
'e'
,因此它将打印
'No Sorry'
,因为
'e'
不在
L

您必须做的是比较元素是否分别位于列表中:

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:

获得输出的诀窍是,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.

因此,当您在[1,3,5]
中计算
(1或2)(实际上您希望[1,3,5]中的
1或[1,3,5]中的
)时,实际发生的是
(1或2)
被计算为
1
,并且您的操作在[1,3,5]中变为
1

如果我想检查列表中是否有n项,是否有“in L:”必须重复n次?是的,否则您要做的是检查“a”或“b”是否为真,并且在Python字符串中,如果不是空的,则计算为真。@BrianLeach我认为有一种更聪明的方法可以做到这一点。。坚持我会想一些更酷的东西。@PeterGoldsborough检查我的答案,
any
就是你要找的函数。“==”和“in L”的行为似乎有所不同。我上面的评论似乎不正确