元组,检查字符串中的字母

发布于 2024-11-29 09:36:38 字数 455 浏览 1 评论 0原文

我有这段代码:

prefixes = "JKLMNOPQ" 
suffix = "ack" 

for letter in prefixes: 
    if letter in ("O", "Q"):
        print letter + "u" + suffix
    else:
        print letter + suffix

它工作正常,但我在理解一件事时遇到问题。我假设:

if letter in ("O", "Q"):

创建包含 2 个字母的新元组:O 和 Q 并检查值字母是否存在。

我不确定为什么这不能正常工作:

if letter == "O" or "Q":

此代码会将“u”添加到所有前缀,而不仅仅是那些带有“O”和“Q”的前缀。

I have this code:

prefixes = "JKLMNOPQ" 
suffix = "ack" 

for letter in prefixes: 
    if letter in ("O", "Q"):
        print letter + "u" + suffix
    else:
        print letter + suffix

It works fine but I have problem understanding one thing. I assume that:

if letter in ("O", "Q"):

creates new tuple with 2 letters: O and Q and checks if value letter is present.

What I'm unsure about is why this won't work correctly:

if letter == "O" or "Q":

This code will add "u" to all prefixes and not just those with "O" and "Q".

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

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

发布评论

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

评论(5

唯憾梦倾城 2024-12-06 09:36:38

所有这些都执行相同的操作:

if letter == "O" or letter == "Q":
if letter in ("O", "Q"):
if letter in "OQ":

您的行 if letter == "O" or "Q": 的计算方式类似于 if (letter == "O") or "Q": code> 和 "Q" 的计算结果为 True,因此该表达式始终返回 True

All these do the same thing:

if letter == "O" or letter == "Q":
if letter in ("O", "Q"):
if letter in "OQ":

Your line if letter == "O" or "Q": is evaluated like if (letter == "O") or "Q":, and "Q" evaluates to True, so this expression always returns True.

泛滥成性 2024-12-06 09:36:38

相等比较优先于“或”运算符: http://docs.python.org/reference /expressions.html#summary

if letter == "O" or "Q":

如果 1) letter 是 "O" 或 2) "Q" 则成功,

因为 "Q" 的计算结果为确实,这个 if 语句总是成功。

Equality comparison takes precedence over "or" operator: http://docs.python.org/reference/expressions.html#summary

if letter == "O" or "Q":

succeeds if 1) letter is "O" or 2) "Q"

Since "Q" evaluates to True, this if statement always succeeds.

不弃不离 2024-12-06 09:36:38
if letter == "O" or "Q":

这可以重写为:

if (letter == "O") or ("Q"):

最后一部分,一个不为空的字符串,始终计算为 True,因此整个表达式为 True

所以你可以这样写:

if letter == "O" or letter == "Q":

或者使用你的元组代码(更好)。

if letter == "O" or "Q":

This can be rewritten to:

if (letter == "O") or ("Q"):

And the last part, a string that is not empty, always evaluates to True, thus your whole expression is True.

So you have write it like this:

if letter == "O" or letter == "Q":

Or use your tuple code (better).

流星番茄 2024-12-06 09:36:38

问题在于:

if letter == "O" or "Q":

它应该是:

if letter == "O" or letter == "Q":

您将“u”添加到所有前缀的原因是因为“Q”始终为真。

The issue lies here:

if letter == "O" or "Q":

It should be:

if letter == "O" or letter == "Q":

The reason you were getting "u" added to all prefixes was because "Q" is always true.

前事休说 2024-12-06 09:36:38

值得注意的是,即使优先级不同,它也不起作用:

if letter == ("O" or "Q"):

这不好,因为 "O" 是 true-ish*,所以 "O" 或 "Q" 是“O”,因此我们只需将字母“O”进行比较,并完全忽略“Q”

在英语中,我们将“O”或“Q”视为一个抽象概念,不确定是“O”还是“Q” > 根据任何逻辑要求。然而,计算机处理的是确定性的数量。我们能得到的最接近英文短语Is the letter "O" or "Q"?(更精确的是,当我们这样做时:))是Is the letter one of以下:“O”,“Q”?我们可以将其形式化为该字母是否在以下集合中:{“O”,“Q”}?

Python允许我们轻松构造集合(使用set) 关键字,但我们仍然必须明确这样一个事实:我们正在检查 letter 是否 in 这个集合(即,我们测试设置成员资格,而不是平等)。幸运的是,我们并不严格要求使用集合来检查成员资格。 (如果我们有大量元素要测试,并且我们只需要构造一次集合,但有很多字母要测试,那么我们可以通过正式创建一个集合来节省总体时间,因为它们'针对成员资格测试进行了重新优化;使用像 ("O", "Q") 这样的元组,Python 需要一次检查每个元素,但在这里, tuple 工作得很好。)

简而言之:你修复了按照其他人所说的方式解决问题,但他们对问题的解释应该让您提出更多问题(我希望我在这里回答)。

It's worth noting that even if the precedence were different, it wouldn't work:

if letter == ("O" or "Q"):

This is no good, since "O" is true-ish*, so "O" or "Q" is "O", so we would just compare letter to "O" and ignore "Q" completely.

In English, we think of "O" or "Q" as an abstract concept that is non-deterministically either "O" or "Q" according to whatever the logic requires. Computers, however, deal in deterministic quantities. The closest we can get to the English phrasing Is the letter "O" or "Q"? (and more precise, while we're at it :) ) is Is the letter one of the following: "O", "Q"? which we can formalize a bit as Is the letter in the following set: {"O", "Q"}?

Python allows us to construct sets easily (with the set) keyword, but we must still be explicit about the fact that we are checking if letter is in this set (i.e., we test for set membership, not equality). Fortunately, we don't strictly require a set to check for membership. (If we had a huge number of elements to test, and we only needed to construct the set once but had many letters to test, then we might save time overall by formally creating a set, since they're optimized for the membership test; with a tuple like ("O", "Q"), Python needs to check each element one at a time. But here, the tuple works just fine.)

In short: you fix the problem the way the others said, but their explanation of the problem ought to leave you asking more questions (which I hope I answered here).

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