开发者

Tuples, checking for a letter in a string

开发者 https://www.devze.com 2023-03-27 11:25 出处:网络
I have this code: prefixes = \"JKLMNOPQ\" suffix = \"ack\" for letter in pr开发者_如何学编程efixes:

I have this code:

prefixes = "JKLMNOPQ" 
suffix = "ack" 

for letter in pr开发者_如何学编程efixes: 
    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".


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.


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.


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).


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号