【问题标题】:if 'a' or 'b' in L, where L is a list (Python) [duplicate]如果 L 中的“a”或“b”,其中 L 是一个列表(Python)[重复]
【发布时间】:2014-02-16 03:58:22
【问题描述】:

我遇到以下逻辑问题:

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


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

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

打印It's there!


列表中只有第一项...

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

打印It's there!


列表中没有项目...

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

打印No sorry


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

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

打印No sorry


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

3、2、1中拍脑门的简单答案...

【问题讨论】:

  • “==”和“in L”的行为似乎不同
  • 我上面的评论好像不正确

标签: python


【解决方案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)

【讨论】:

  • 如果我想检查n个项目是否在列表中,“in L:”是否必须重复n次?
  • 是的,否则您要做的是检查“a”或“b”是真还是假,并且在非空的 Python 字符串中计算为真。
  • @BrianLeach 我认为有一种更聪明的方法可以做到这一点……等等。我会想一些更酷的东西。
  • @PeterGoldsborough 看看我的回答,any 是您正在寻找的功能。
【解决方案2】:

让我们分解表达式:

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

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

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

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

【讨论】:

    【解决方案3】:

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

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

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

    当你这样做时

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

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

    所以当你这样做时

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

    'e' or '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:
    

    【讨论】:

      【解决方案4】:

      你得到的输出的诀窍是 Python 中的 andor 总是计算它们的操作数之一——通常是最后必须计算的一个,以确定操作的真实性:

      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 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]

      【讨论】:

        猜你喜欢
        • 2020-01-02
        • 2019-12-03
        • 2021-09-21
        • 2015-04-29
        • 1970-01-01
        • 2019-12-05
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        相关资源
        最近更新 更多