Python 如何检查两个值之间的变化(百分比)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30926840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check change between two values (in percent)?
提问by Nips
I have two values (previous and current) and I want to check change between them (in percent):
我有两个值(以前的和当前的),我想检查它们之间的变化(百分比):
(current/previous)*100
But if the previous value is 0 I get Division By Zero, and if value will not change I get 100%.
但是如果之前的值是 0,我会得到除以零,如果值不会改变,我会得到 100%。
采纳答案by Matt
def get_change(current, previous):
if current == previous:
return 100.0
try:
return (abs(current - previous) / previous) * 100.0
except ZeroDivisionError:
return 0
Edit: some have commented that OP was describing a problem with the current code, not asking for this behavior, thus here is an example where, "if the current is equal to previous, there is no change. You should return 0". Also I've made the method return Infinity if the previous value was 0, as there can be no real percentage change when the original value is 0.
编辑:有些人评论说 OP 是在描述当前代码的问题,而不是要求这种行为,因此这里是一个示例,“如果当前等于前一个,则没有变化。您应该返回 0”。此外,如果前一个值为 0,我已经使该方法返回 Infinity,因为当原始值为 0 时,不会有真正的百分比变化。
def get_change(current, previous):
if current == previous:
return 0
try:
return (abs(current - previous) / previous) * 100.0
except ZeroDivisionError:
return float('inf')
回答by Mureinik
You need to divide the change (current-previous
) by the previous
, not just the current. So, to make a long story short:
您需要将变化 ( current-previous
) 除以previous
,而不仅仅是电流。所以,长话短说:
change_percent = ((float(current)-previous)/previous)*100
Note that if previous
is 0
you cannot calculate the change in percentage (regardless of the python implementation)
请注意,如果previous
是0
,则无法计算百分比的变化(无论 Python 实现如何)
回答by Brien
To cover all cases of zeroes, you could use ternary operators in your statement
要涵盖所有零的情况,您可以在语句中使用三元运算符
(current - previous) / previous * 100.0 if previous != 0 else float("inf") * abs(current) / current if current != 0 else 0.0
回答by Nikos Tavoularis
You should divide by the absolute value of the previous number. If the previous number is negative and the current number is negative you will get an erroneous result if you don't use the absolute value in the denominator. For example if your current number is -6 and previous number is -5:
您应该除以前一个数字的绝对值。如果前一个数字是负数而当前数字是负数,如果您不使用分母中的绝对值,您将得到错误的结果。例如,如果您当前的号码是 -6,而之前的号码是 -5:
(-6 - (-5)) / -5 =
(-6 + 5) / -5 =
-1 / -5 = 20 %
which is clearly false because the percentage change in this case should be negative -6 < -5
. So use the function below:
这显然是错误的,因为这种情况下的百分比变化应该是负的-6 < -5
。所以使用下面的函数:
def percentage_change(current, previous):
if previous != 0 :
return float(current - previous) / abs(previous) * 100
else:
return "undefined"
Keep in mind that if your previous number is zero division by zero is undefined: https://en.wikipedia.org/wiki/Division_by_zero
请记住,如果您之前的数字为零除以零是未定义的:https: //en.wikipedia.org/wiki/Division_by_zero
Also you shouldn't use the absolute value of the nominator instead of the denominator. Here is an example why:
此外,您不应该使用分母的绝对值而不是分母。这是一个例子:
previous value: -5
先前值:-5
current value: -4
当前值:-4
| (-4 - (-5)) | / -5 =
| (-4 + 5) | / -5 =
|1| / -5 =
1 / -5 = -20%
which is false because -4 > -5
这是错误的,因为 -4 > -5
Correct:
正确的:
(-4 - (-5)) / | -5 | =
(-4 + 5) / | -5 | =
1 / 5 = 20%
回答by Caner ?akmak
I use this
我用这个
def pct_change(first, second):
diff = second - first
change = 0
try:
if diff > 0:
change = (diff / first) * 100
elif diff < 0:
diff = first - second
change = -((diff / first) * 100)
except ZeroDivisionError:
return float('inf')
return change