元组,检查字符串中的字母
我有这段代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所有这些都执行相同的操作:
您的行
if letter == "O" or "Q":
的计算方式类似于if (letter == "O") or "Q":
code> 和"Q"
的计算结果为True
,因此该表达式始终返回True
。All these do the same thing:
Your line
if letter == "O" or "Q":
is evaluated likeif (letter == "O") or "Q":
, and"Q"
evaluates toTrue
, so this expression always returnsTrue
.相等比较优先于“或”运算符: 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.
这可以重写为:
最后一部分,一个不为空的字符串,始终计算为
True
,因此整个表达式为True
。所以你可以这样写:
或者使用你的元组代码(更好)。
This can be rewritten to:
And the last part, a string that is not empty, always evaluates to
True
, thus your whole expression isTrue
.So you have write it like this:
Or use your tuple code (better).
问题在于:
它应该是:
您将“u”添加到所有前缀的原因是因为“Q”始终为真。
The issue lies here:
It should be:
The reason you were getting "u" added to all prefixes was because "Q" is always true.
值得注意的是,即使优先级不同,它也不起作用:
这不好,因为
"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:
This is no good, since
"O"
is true-ish*, so"O" or "Q"
is "O", so we would just compareletter
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 phrasingIs the letter "O" or "Q"?
(and more precise, while we're at it :) ) isIs the letter one of the following: "O", "Q"?
which we can formalize a bit asIs 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 ifletter
isin
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 aset
, since they're optimized for the membership test; with atuple
like("O", "Q")
, Python needs to check each element one at a time. But here, thetuple
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).