“x不在y中”或“y中没有x”

发布于 2024-12-25 03:32:26 字数 253 浏览 1 评论 0 原文

在测试成员资格时,我们可以使用:

x not in y

或者:

not x in y

该表达式可以有许多可能的上下文,具体取决于 xy。例如,它可以用于子字符串检查、列表成员身份、字典键是否存在。

  • 这两种形式总是等价的吗?
  • 有首选语法吗?

When testing for membership, we can use:

x not in y

Or alternatively:

not x in y

There can be many possible contexts for this expression depending on x and y. It could be for a substring check, list membership, dict key existence, for example.

  • Are the two forms always equivalent?
  • Is there a preferred syntax?

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

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

发布评论

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

评论(6

埋葬我深情 2025-01-01 03:32:26

他们总是给出相同的结果。

事实上,not 'ham' in 'spam and Eggs' 似乎是执行单个“not in”操作的特殊情况,而不是执行“in”操作然后否定结果:

>>> import dis

>>> def notin():
    'ham' not in 'spam and eggs'
>>> dis.dis(notin)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE    

>>> def not_in():
    not 'ham' in 'spam and eggs'
>>> dis.dis(not_in)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE    

>>> def not__in():
    not ('ham' in 'spam and eggs')
>>> dis.dis(not__in)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        

>>> def noteq():
    not 'ham' == 'spam and eggs'
>>> dis.dis(noteq)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               2 (==)
              9 UNARY_NOT           
             10 POP_TOP             
             11 LOAD_CONST               0 (None)
             14 RETURN_VALUE      

我有起初认为它们总是给出相同的结果,但 not 本身只是一个低优先级逻辑否定运算符,它可以应用于 a in b 就像与任何其他布尔表达式一样容易,而为了方便和清晰起见,not in 是一个单独的运算符。

上面的拆解已经暴露了!看起来,虽然 not 显然是一个逻辑否定运算符,但 not a in b 的形式是特殊情况,因此它实际上并未使用通用运算符。这使得 not a in b 在字面上与 a not in b 表达式相同,而不仅仅是产生相同值的表达式。

They always give the same result.

In fact, not 'ham' in 'spam and eggs' appears to be special cased to perform a single "not in" operation, rather than an "in" operation and then negating the result:

>>> import dis

>>> def notin():
    'ham' not in 'spam and eggs'
>>> dis.dis(notin)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE    

>>> def not_in():
    not 'ham' in 'spam and eggs'
>>> dis.dis(not_in)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE    

>>> def not__in():
    not ('ham' in 'spam and eggs')
>>> dis.dis(not__in)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        

>>> def noteq():
    not 'ham' == 'spam and eggs'
>>> dis.dis(noteq)
  2           0 LOAD_CONST               1 ('ham')
              3 LOAD_CONST               2 ('spam and eggs')
              6 COMPARE_OP               2 (==)
              9 UNARY_NOT           
             10 POP_TOP             
             11 LOAD_CONST               0 (None)
             14 RETURN_VALUE      

I had thought at first that they always gave the same result, but that not on its own was simply a low precedence logical negation operator, which could be applied to a in b just as easily as any other boolean expression, whereas not in was a separate operator for convenience and clarity.

The disassembly above was revealing! It seems that while not obviously is a logical negation operator, the form not a in b is special cased so that it's not actually using the general operator. This makes not a in b literally the same expression as a not in b, rather than merely an expression that results in the same value.

吾家有女初长成 2025-01-01 03:32:26
  1. 不,没有区别。

    <块引用>

    运算符not in被定义为具有in的逆真值。

    Python 文档

  2. 我认为 not in 是首选,因为它更明显他们还为其添加了一个特殊情况。
  1. No, there is no difference.

    The operator not in is defined to have the inverse true value of in.

    Python documentation

  2. I would assume not in is preferred because it is more obvious and they added a special case for it.
卸妝后依然美 2025-01-01 03:32:26

它们的含义相同,但 pycodestyle Python 样式指南检查器(以前称为 pep8) 更喜欢 规则 E713 中的 >not in 运算符:

E713:成员资格测试应该不在

另请参阅“Python if x is not Noneif not x is None?” 用于非常相似的样式选择。

They are identical in meaning, but the pycodestyle Python style guide checker (formerly called pep8) prefers the not in operator in rule E713:

E713: test for membership should be not in

See also "Python if x is not None or if not x is None?" for a very similar choice of style.

命硬 2025-01-01 03:32:26

其他人已经非常清楚地表明,这两种说法在相当低的水平上是等效的。

但是,我认为还没有人充分强调,由于这将选择权留给了您,因此您应该

选择使代码尽可能具有可读性的形式。

并且不一定像那样具有可读性对任何人来说都是可能的,即使这当然是一件好事。不,请确保代码对您来说尽可能可读,因为您是最有可能稍后再回到此代码并尝试阅读它的人。

Others have already made it very clear that the two statements are, down to a quite low level, equivalent.

However, I don't think that anyone yet has stressed enough that since this leaves the choice up to you, you should

choose the form that makes your code as readable as possible.

And not necessarily as readable as possible to anyone, even if that's of course a nice thing to aim for. No, make sure the code is as readable as possible to you, since you are the one who is the most likely to come back to this code later and try to read it.

哎呦我呸! 2025-01-01 03:32:26

在Python中,没有区别。并且没有任何偏好。

In Python, there is no difference. And there is no preference.

半仙 2025-01-01 03:32:26

从语法上讲,它们是相同的语句。我会很快指出,“火腿”不在“垃圾邮件和鸡蛋”中传达了更清晰的意图,但我已经看到了代码和场景,其中“火腿”不在“垃圾邮件和鸡蛋”中' 传达的含义比另一个更清晰。

Syntactically they're the same statement. I would be quick to state that 'ham' not in 'spam and eggs' conveys clearer intent, but I've seen code and scenarios in which not 'ham' in 'spam and eggs' conveys a clearer meaning than the other.

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