我正在尝试编写一个函数,将多个变量与一个整数进行比较,并输出一个由三个字母组成的字符串。我想知道有没有办法把它翻译成Python。所以说:
x = 0
y = 1
z = 3
mylist = []
if x or y or z == 0:
mylist.append("c")
if x or y or z == 1:
mylist.append("d")
if x or y or z == 2:
mylist.append("e")
if x or y or z == 3:
mylist.append("f")
它将返回一个列表,其中包括:
["c", "d", "f"]
30条答案
按热度按时间sf6xfgos1#
The most pythonic way of representing your pseudo-code in Python would be:
0x6upsns2#
now we can useaas [row,col] selector, which acts as if any(...) condition :
xsuvu9jc3#
FIRST, A CORRECTION TO THE
OR
CONDITIONAL:You need to say:
The reason is that "or" splits up the condition into separate logical parts. The way your original statement was written, those parts were:
The last part was fine --- checking to see if z == 0, for instance --- but the first two parts just said essentially
if x
andif y
. Since integers always evaluate toTrue
unless they're 0, that means the first part of your condition was alwaysTrue
whenx
ory
didn't equal 0 (which in the case of y was always, since you hady = 1
, causing your whole condition (because of howOR
works) to always beTrue
.To avoid that, you need to make sure all parts of your condition (each side of the
OR
) make sense on their own (you can do that by pretending that the other side(s) of theOR
statement doesn't exist). That's how you can confirm whether or not yourOR
condition is correctly defined.You would write the statements individually like so:
which means the correct mergin with the
OR
keyword would be:SECOND, HOW TO SOLVE THE PROBLEM:
You're basically wanting to check to see if any of the variables match a given integer and if so, assign it a letter that matches it in a one-to-one mapping. You want to do that for a certain list of integers so that the output is a list of letters. You'd do that like this:
Similarly, you could use LIST COMPREHENSION to achieve the same result faster:
myss37ts4#
usage without if example:
rxztt3cl5#
Here is one more way to do it:
It is a mix oflist comprehensionandanykeyword.
jc3wubiy6#
Problem
While the pattern for testing multiple values
is very readable and is working in many situation, there is one pitfall:
But we want to have
Solution
One generalization of the previous expression is based on the answer from ytpillai:
which can be written as
While this expression returns the right result it is not as readable as the first expression :-(
gwo2fgha7#
The
or
does not work like that, as explained by this answer.While the generic answer would be use
this is not the best one for the specific problem. In your case you're doing repeated tests, therefore it is worthwhile to compose a set of these variables:
We can simplify this using a dictionary - this will result in the same values:
Or if the ordering of the
mylist
is arbitrary, you can loop over the values instead and match them to the mappings:h7wcgrx38#
you can develop it through two ways
Or
mw3dktmi9#
You can unite this
in one variable.
Change our conditions as:
Output:
sf6xfgos10#
This will help you.
ovfsdjhp11#
Without dict, try this solution:
and gives:
vsaztqbk12#
You can use dictionary :
qnzebej013#
Looks like you're building some kind of Caesar cipher.
A much more generalized approach is this:
outputs
Not sure if it's a desired side effect of your code, but the order of your output will always be sorted.
If this is what you want, the final line can be changed to:
eoigrqb614#
To test multiple variables with one single value:
if 1 in {a,b,c}:
To test multiple values with one variable:
if a in {1, 2, 3}:
rbpvctlc15#
It can be done easily as