I am having trouble with the following logic:
我在以下逻辑上遇到了麻烦:
Lets say I have a list:
假设我有一份清单:
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!
指纹就在那里!
Only the first item is in the list...
只有第一项在列表中...
if ('a' or 'd') in L:
prints It's there!
指纹就在那里!
Neither item in the list...
列表中的两个项目都不是...
if ('e' or 'd') in L:
prints No sorry
打印,不,抱歉
Here's the confusing one. Only the second item in the list...
这是一个令人困惑的问题。只有列表中的第二个项目...
if ('e' or 'a') in L:
prints No sorry
打印,不,抱歉
I do not understand why this is not registering as a true statement. How does this generalize to an or statement with n conditionals?
我不明白为什么这不是一个真实的陈述。这如何推广到带有n个条件句的or语句?
更多回答
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’或‘a’)将首先检查‘e’是否为True。如果是,则该表达式将返回‘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’。这意味着在L中的if(‘e’或‘a’)在L中可以被翻译为if‘e’,在本例中是假的。
A more generic way to check if a list contains at least one value of a set of values, is to use the any
function coupled with a generator expression.
检查列表是否至少包含一组值中的一个值的一种更通用的方法是将any函数与生成器表达式结合使用。
if any(c in L for c in ('a', 'e')):
Use this instead:
请改用以下内容:
if 'a' in L or 'b' in L:
If we want to check if all these of this "items" are in the list, all
and a generator comprehension is your friend:
如果我们想要检查是否所有这些“项目”都在列表中,所有的生成器理解都是你的朋友:
items = 'a', 'b', 'c'
if all(i in L for i in items):
Or if any of these items are in the list, use any
:
或者,如果列表中有这些项目中的任何一项,请使用以下任何一项:
if any(i in L for i in items)
Strings (except an empy string) will always evaluate to True
when they are evaluated as a boolean. While evaluating with or/and
both will return True
, but there is a little difference between them:
字符串(空字符串除外)在作为布尔值进行计算时,其计算结果始终为True。虽然使用或/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:将返回第一个字符串,而:将返回最后一个字符串
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’或‘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 not in L
.
,‘e’或‘a’的计算结果将为‘e’,因此它将打印‘No抱歉’,因为‘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:
The trick to the output you're getting is that and
and or
in Python always evaluate to one of their operands -- generally the one that had to be evaluated last to determine the truthiness of the operation:
您得到的输出的诀窍在于,在Python中,AND和OR总是计算它们的一个操作数--通常是最后必须计算的那个操作数,以确定操作的真实性:
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,3,5]中计算(1或2)时(实际上你想要[1,3,5]中的1或[1,3,5]中的2),实际发生的情况是(1或2)被求值为1,并且你的运算在[1,3,5]中变为1。
更多回答
我是一名优秀的程序员,十分优秀!