【发布时间】:2016-12-15 12:18:47
【问题描述】:
在尝试消除字符串列表中的几个字符串时,我尝试使用类似于以下的简单代码:
>>> s = ['a b', 'c d', 'e f', 'g h']
>>> for i in s:
... if i is not 'e f':
... print(i)
...
a b
c d
e f # this should not get printed, right?
g h
我无法理解潜在的行为? 你能解释一下吗?因为以下似乎合乎逻辑,以上也应该相应地工作
>>> if 'a b' is not 'a b':
... True
... else:
... False
...
False
>>> s = ['a', 'c', 'e', 'g']
>>> for i in s:
... if i is not 'e':
... print(i)
...
a
c
g
需要特殊处理的空间吗?我错过了什么?
【问题讨论】:
标签: python string python-3.x string-comparison