Python:!= 和“is not”之间的区别

发布于 2024-11-03 13:23:23 字数 482 浏览 0 评论 0原文

我不清楚语法 !=is not 之间的区别。它们似乎做了同样的事情:

>>> s = 'a'
>>> s != 'a'
False
>>> s is not 'a'
False

但是,当我在列表理解中使用 is not 时,它会产生与使用 != 不同的结果。

>>> s = "hello"
>>> [c for c in s if c is not 'o']
['h', 'e', 'l', 'l', 'o']
>>> [c for c in s if c != 'o']
['h', 'e', 'l', 'l']

为什么 o 包含在第一个列表中,而不是第二个列表中?

I'm unclear about the difference between the syntax != and is not. They appear to do the same thing:

>>> s = 'a'
>>> s != 'a'
False
>>> s is not 'a'
False

But, when I use is not in a list comprehension, it produces a different result than if I use !=.

>>> s = "hello"
>>> [c for c in s if c is not 'o']
['h', 'e', 'l', 'l', 'o']
>>> [c for c in s if c != 'o']
['h', 'e', 'l', 'l']

Why did the o get included in the first list, but not the second list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云裳 2024-11-10 13:23:23

is 测试对象身份,但 == 测试对象值相等:

In [1]: a = 3424
In [2]: b = 3424

In [3]: a is b
Out[3]: False

In [4]: a == b
Out[4]: True

is tests for object identity, but == tests for object value equality:

In [1]: a = 3424
In [2]: b = 3424

In [3]: a is b
Out[3]: False

In [4]: a == b
Out[4]: True
陈年往事 2024-11-10 13:23:23

is not 比较引用。 == 比较值

is not compares references. == compares values

柏林苍穹下 2024-11-10 13:23:23

根据您的困惑程度,这可能会有所帮助。

这些语句是相同的:

[c for c in s if c != 'o']
[c for c in s if not c == 'o']

Depending on how you were confused, this might help.

These statements are the same:

[c for c in s if c != 'o']
[c for c in s if not c == 'o']
反话 2024-11-10 13:23:23

我想补充一点,他们绝对不会做同样的事情。
我会用!=。
例如,如果你有一个 unicode 字符串......

a = u'hello'
'hello' is not a
True
'hello' != a
False

使用 !=,Python 基本上会执行从 str() 到 unicode() 的隐式转换并比较它们,而使用 is 则不会,如果它是完全相同的实例,则它会匹配。

I'd like to add that they definitely do not do the same thing.
I would use !=.
For instance if you have a unicode string....

a = u'hello'
'hello' is not a
True
'hello' != a
False

With !=, Python basically performs an implicit conversion from str() to unicode() and compares them, whereas with is not, it matches if it is exactly the same instance.

烂柯人 2024-11-10 13:23:23

我只是引用参考资料
is 测试操作数是否相同,可能引用同一个对象。其中 != 测试该值。

s = [1,2,3]
while s is not []:
   s.pop(0);

这是一个不定循环,因为对象 s 永远不等于 [] 所引用的对象,它引用的是一个完全不同的对象。其中,用 s != [] 替换条件将使循环确定,因为这里我们正在比较值,当 s 中的所有值都弹出后,剩下的就是一个空列表。

I am just quoting from reference,
is tests whether operands are one and same, probably referring to the same object. where as != tests for the value.

s = [1,2,3]
while s is not []:
   s.pop(0);

this is a indefinite loop, because object s is never equal to an object reference by [], it refers to a completely different object. where as replacing the condition with s != [] will make the loop definite, because here we are comparing values, when all the values in s are pop'd out what remains is a empty list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文