我正在尝试编写一个函数,将多个变量与一个整数进行比较,并输出一个由三个字母组成的字符串。我想知道有没有办法把它翻译成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条答案
按热度按时间mnemlml81#
您误解了布尔表达式的工作原理;它们不像英语句子那样工作,并且猜测您在这里谈论的是对所有名称的相同比较。您正在寻找:
x
和y
以其他方式单独计算(如果是0
,则为False
,否则为True
)。您可以使用针对元组的包容测试来缩短该时间:
或者更好的是:
使用
set
来利用不变成本成员关系测试(即,无论左操作数是什么,in
都需要固定的时间)。说明
当您使用
or
时,Python将运算符的每一端视为单独的表达式。表达式x or y == 1
首先被视为对x
的布尔测试,如果为假,则对表达式y == 1
进行测试。这是由于运算符优先。
or
运算符的优先级低于==
测试,因此后者被首先评估。然而,即使情况并非如此,表达式
x or y or z == 1
实际上被解释为(x or y or z) == 1
,这仍然不会达到您预期的效果。x or y or z
将计算为第一个‘true’参数,例如,不是False
、数字0或空(有关在布尔上下文中,Python认为是假的详细信息,请参阅布尔表达式)。因此,对于值
x = 2; y = 1; z = 0
,x or y or z
将解析为2
,因为这是参数中第一个类似于真的值。则2 == 1
将是False
,即使y == 1
将是True
。反之亦然;针对单个变量测试多个值;
x == 1 or 2 or 3
将因同样的原因而失败。使用x == 1 or x == 2 or x == 3
或x in {1, 2, 3}
。qyuhtwio2#
使用如下词典结构可以更轻松地解决您的问题:
falq053o3#
正如Martjn Pieters所说,正确且最快的格式是:
按照他的建议,您现在可以使用单独的if-语句,这样,无论前者是
True
还是False
,Python都将读取每个语句。例如:这是可行的,但如果您习惯于使用字典(参见我在那里所做的),您可以通过创建一个将数字Map到您想要的字母的初始字典,然后只需使用for循环来清理这一问题:
qoefvg9y4#
The direct way to write
x or y or z == 0
isBut I dont think, you like it. :) And this way is ugly.
The other way (a better) is:
BTW lots of
if
s could be written as something like thispdtvr36n5#
If you ARE very very lazy, you can put the values inside an array. Such as
You can also put the numbers and letters in a dictionary and do it, but this is probably a LOT more complicated than simply if statements. That's what you get for trying to be extra lazy :)
One more thing, your
will compile, but not in the way you want it to. When you simply put a variable in an if statement (example)
the program will check if the variable is not null. Another way to write the above statement (which makes more sense) is
Bool is an inbuilt function in python which basically does the command of verifying a boolean statement (If you don't know what that is, it is what you are trying to make in your if statement right now :))
Another lazy way I found is :
vxf3dgd46#
To check if a value is contained within a set of variables you can use the inbuilt modules
itertools
andoperator
.For example:
Imports:
Declare variables:
Create mapping of values (in the order you want to check):
Use
itertools
to allow repetition of the variables:Finally, use the
map
function to create an iterator:Then, when checking for the values (in the original order), use
next()
:etc...
This has an advantage over the
lambda x: x in (variables)
becauseoperator
is an inbuilt module and is faster and more efficient than usinglambda
which has to create a custom in-place function.Another option for checking if there is a non-zero (or False) value in a list:
Equivalent:
ig9co6j17#
Set is the good approach here, because it orders the variables, what seems to be your goal here.
{z,y,x}
is{0,1,3}
whatever the order of the parameters.This way, the whole solution is O(n).
gcxthw6b8#
I think this will handle it better:
Output:
tvmytwxo9#
If you want to use if, else statements following is another solution:
2uluyalo10#
All of the excellent answers provided here concentrate on the specific requirement of the original poster and concentrate on the
if 1 in {x,y,z}
solution put forward by Martijn Pieters.What they ignore is the broader implication of the question:
How do I test one variable against multiple values?
The solution provided will not work for partial hits if using strings for example:
Test if the string "Wild" is in multiple values
or
for this scenario it's easiest to convert to a string
It should be noted however, as mentioned by
@codeforester
, that word boundries are lost with this method, as in:the 3 letters
rot
do exist in combination in the list but not as an individual word. Testing for " rot " would fail but if one of the list items were "rot in hell", that would fail as well.The upshot being, be careful with your search criteria if using this method and be aware that it does have this limitation.
6rqinv9w11#
ve7v8dk212#
This code may be helpful
qyzbxkaa13#
You can try the method shown below. In this method, you will have the freedom to specify/input the number of variables that you wish to enter.
fcwjkofz14#
One line solution:
Or:
chhqkbe115#
Maybe you need direct formula for output bits set.
Let's map to bits:
'c':1 'd':0xb10 'e':0xb100 'f':0xb1000
Relation of isc (is 'c'):
Use math if formula https://youtu.be/KAdKCgBGK0k?list=PLnI9xbPdZUAmUL8htSl6vToPQRRN3hhFp&t=315
[c]:
(xyz=0 and isc=1) or (((xyz=0 and isc=1) or (isc=0)) and (isc=0))
[d]:
((x-1)(y-1)(z-1)=0 and isc=2) or (((xyz=0 and isd=2) or (isc=0)) and (isc=0))
...
Connect these formulas by following logic:
and
is the sum of squares of equationsor
is the product of equationsand you'll have a total equation express sum and you have total formula of sum
then sum&1 is c, sum&2 is d, sum&4 is e, sum&5 is f
After this you may form predefined array where index of string elements would correspond to ready string.
array[sum]
gives you the string.