【发布时间】:2014-06-23 19:21:57
【问题描述】:
我知道这是一个基本问题,但请多多包涵。假设我们下面有 4 个字符串:
a = ''
b = 'apple'
c = 'orange'
d = 'banana'
所以,通常如果我想检查三个字符串 a b c 是否为空,我可以使用 len() 函数。
if len(a) == 0 or len(b) == 0 or len(c) == 0:
return True
但后来我觉得如果我有很多字符串,像上面这样写太麻烦了。所以,我用了
if not a:
return True
但是,当我使用上述方法检查多个字符串 b c d 时,它返回 True 我很困惑,因为不是字符串 b c d 其中空。
if not b or c or d:
return True
发生了什么事?
【问题讨论】:
-
想解释一下投反对票的原因吗?
-
我怀疑您可能对
or运算符期望过高。见stackoverflow.com/questions/15112125/if-x-or-y-or-z-blah。目前尚不清楚这是否是优先级混淆、对or真正作用的误解,还是其他原因。
标签: python string if-statement