[英]How to compute a percentage increase/decrease multiplier in python without if statements
I have a scenario in which the user enter a positive, 0, or negative value which represents a percentage increase, no change, or percentage decrease, respectively. 我有一种情况,用户输入一个正值,0或负值,分别代表增加百分比,没有变化或减少百分比。 Example: 2.5 means "increase by 2.5%", -3 means "decrease by 3%". 示例:2.5表示“增加2.5%”,-3表示“减少3%”。 Let's call this user entered number U. 将此用户称为输入的数字U。
I need to apply U to some number, X, in python... that is, apply the percentage increase/decrease specified in U to X to come up with the possibly altered value for X. 我需要在python中将U应用于某个数字X,也就是说,将U中指定的百分比增加/减少应用于X,以得出X可能更改的值。
I can see one way to do this is by examining U and come up with a multiplier that can be multiplied by X, such as 我可以看到执行此操作的一种方法是检查U并得出可以乘以X的乘数,例如
if U == 0:
multiplier = 1
elif U > 0:
multiplier = U/100.0 + 1
else:
multiplier = (100 - (U * -1.0))/100.0
And then I can arrive at the new value of X with: X = X * multiplier. 然后,我可以得出X的新值:X = X *乘数。
Finally, the challenging part: Is there a way to come up with a multiplier without using multiple if statements as I've done above? 最后,具有挑战性的部分: 是否有一种方法可以得出乘法器而不像我上面那样使用多个if语句?
The reason I ask is because I'm actually having to write a snippet of python code that has to be contained in one line (it is is going to be dynamically executed with exec()) and thus I can't have blocks. 我问的原因是因为我实际上必须编写一小段必须包含在一行中的python代码(它将通过exec()动态执行),因此我无法使用代码块。
Any ideas on this would be greatly appreciated! 任何想法,将不胜感激! Michael 麦可
You just need to use the distributive property of division: 您只需要使用除法的分配属性:
multiplier = 1 + U/100.0
This is actually what your code does . 这实际上就是您的代码所做的 。 You just separated three branches of a if
that did exactly the same. 您只是将a的三个分支分开, if
这样做完全相同。 Let's do some easy math step-by-step from your original code: 让我们从原始代码中逐步进行一些简单的数学运算:
if U == 0:
multiplier = 1
elif U > 0:
multiplier = U/100.0 + 1
else:
multiplier = (100 - (U * -1.0))/100.0
if U == 0:
multiplier = 1 + 0 #sum 0 for convenience
elif U > 0:
multiplier = 1 + U/100.0 #swap order of addition
else:
multiplier = (100 + U)) / 100.0 #multiplying by -1 is just changing the sign
if U == 0:
multiplier = 1 + U/100.0 # since U is 0 in this branch, U/100.0 == 0
elif U > 0:
multiplier = 1 + U/100.0
else:
multiplier = 1 + U/100.0 # distributive property of division
Since the executed code is the same for the three branches, there's no point having a if
: 由于三个分支的执行代码相同,因此没有if
:
multiplier = 1 + U/100.0
if and else can actually be used in expressions on one line: x = a if b else c
. if和else实际上可以在一行的表达式中使用: x = a if b else c
。 You can't use elif, though, so you'll have to first refactor your code so it doesn't use them: 但是,您不能使用elif,因此必须首先重构代码,以使其不使用它们:
if U == 0:
multiplier = 1
else:
if U > 0:
multiplier = U/100.0 + 1
else:
multiplier = (100 - (U * -1.0))/100.0
This reduces to 这减少到
if U == 0:
multiplier = 1
else:
multiplier = U/100.0 + 1 if U > 0 else (100 - (U * -1.0))/100.0
Which reduces to 减少到
multiplier = 1 if U == 0 else (U/100.0 + 1 if U > 0 else (100 - (U * -1.0))/100.0)
Kevin's answer is technically completely correct, but your code can be simplified a whole lot just from a math standpoint. 从技术上来说,凯文的答案是完全正确的,但仅从数学角度来看,您的代码就可以大大简化。
You don't need to be performing if-then-else checks on this mathematical operation. 您无需对此数学运算执行if-then-else检查。 Consider the input of "-3": In your code, this will go into the third block, evaluate as (100 - (-3 * -1.0)) / 100.0 = (100 - 3.0) / 100.0 = 0.97. 考虑输入“ -3”:在您的代码中,该代码将进入第三个块,其值为(100-(-3 * -1.0))/ 100.0 =(100-3.0)/ 100.0 = 0.97。
This is equivalent to the value U going into the second block, evaluated as (-3 / 100.0) + 1 = -0.03 + 1 = 0.97. 这等效于进入第二个块的值U,其值为(-3 / 100.0)+1 = -0.03 +1 = 0.97。
Now consider the input of 0: No matter which block it goes in to, the "multiplier" value will come out as 1. 现在考虑输入0:无论进入哪个块,“乘数”值都将为1。
So just have your code be: 因此,将您的代码设为:
multiplier = 1 + (U / 100.0)
and you'll be set. 就会被设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.