I am trying to write a programme in Python 3which calculates the mean of the absolute differences between successive values.
我正在尝试用Python 3编写一个程序,它计算连续值之间绝对差值的平均值。
4 个解决方案
#1
6
EDIT: As the code was removed from the question, updating answer, moving the issues with code to bottom.
编辑:当代码从问题中删除,更新答案,将代码问题移到底部。
As given in comments you can use enumerate()
to get the index as well as element from the array and then use that to calculate the mean. Example -
如注释中所示,您可以使用enumerate()来获取索引以及数组中的元素,然后使用它来计算平均值。示例 -
>>> def absolute_difference(v):
... sum_diff = 0
... for i,x in enumerate(v):
... if i+1 < len(v):
... sum_diff += abs(v[i+1] - x)
... r = sum_diff/len(v)
... return r
...
>>> absolute_difference([4.0, 4.2, 4.1, 4.4, 4.3])
0.1400000000000004
Lots of issues in the code (that you seem to have removed) -
代码中的很多问题(您似乎已删除) -
-
Why are you converting your absolute difference to
float
? float mathematics is not accurate , as you can see from the sum of difference in your code -0.20000000000000018
. In your case you do not need to convert them to float.你为什么把你的绝对差异转换为浮动?浮动数学是不准确的,你可以从代码中的差异总和看出 - 0.20000000000000018。在您的情况下,您不需要将它们转换为浮动。
-
The main issue of
0.0
forr
occurs because you are using//
to divide,//
truncates the division to the nearest integer, so diving7.0
by something grater than that using//
would always result in0.0
. Example -出现0.0的主要问题是因为你使用//来划分,//将除法截断为最接近的整数,所以以比使用//更大的方式跳过7.0将总是得到0.0。示例 -
>>> 7.0 // 8.0 0.0 >>> 7.0/8.0 0.875
For your case, you should divide using
/
.对于您的情况,您应该使用/来划分。
-
You are taking the mean in each iteration of the loop, though that is not an issue , it may not be completely needed. If you do not want to take the mean in each iteration of the loop, you should indent it outside the loop.
你在循环的每次迭代中取平均值,虽然这不是问题,但可能并不完全需要。如果你不想在循环的每次迭代中取平均值,你应该在循环之外缩进它。
#2
1
You are using //
which means integer division in python 3
你正在使用//这意味着python 3中的整数除法
That is
那是
i.e)
即)
2/4 =0.5
2//4=0
Just replace the //
with /
when calculating r
只需在计算r时用//替换//
#3
0
Here is another approach:
这是另一种方法:
def absolute_difference(values):
last = values[0]
total = 0
for value in values[1:]:
total += abs(value - last)
last = value
return total/(len(values)-1)
print('{:.5f}'.format(absolute_difference([4.0, 4.2, 4.1, 4.4, 4.3])))
Giving the answer: 0.17500
给出答案:0.17500
#4
0
Also, to prevent None
from appearing at the end, you must have return
at the end of your definition. This happens if you make another variable "equal to" (=
) your definition. This was shown in the other posts, but I'm stating this just to highlight things out.
另外,为了防止None出现在最后,您必须在定义的末尾返回。如果您将另一个变量“等于”(=)定义,则会发生这种情况。这在其他帖子中有所体现,但我说这只是为了强调一些事情。
#1
6
EDIT: As the code was removed from the question, updating answer, moving the issues with code to bottom.
编辑:当代码从问题中删除,更新答案,将代码问题移到底部。
As given in comments you can use enumerate()
to get the index as well as element from the array and then use that to calculate the mean. Example -
如注释中所示,您可以使用enumerate()来获取索引以及数组中的元素,然后使用它来计算平均值。示例 -
>>> def absolute_difference(v):
... sum_diff = 0
... for i,x in enumerate(v):
... if i+1 < len(v):
... sum_diff += abs(v[i+1] - x)
... r = sum_diff/len(v)
... return r
...
>>> absolute_difference([4.0, 4.2, 4.1, 4.4, 4.3])
0.1400000000000004
Lots of issues in the code (that you seem to have removed) -
代码中的很多问题(您似乎已删除) -
-
Why are you converting your absolute difference to
float
? float mathematics is not accurate , as you can see from the sum of difference in your code -0.20000000000000018
. In your case you do not need to convert them to float.你为什么把你的绝对差异转换为浮动?浮动数学是不准确的,你可以从代码中的差异总和看出 - 0.20000000000000018。在您的情况下,您不需要将它们转换为浮动。
-
The main issue of
0.0
forr
occurs because you are using//
to divide,//
truncates the division to the nearest integer, so diving7.0
by something grater than that using//
would always result in0.0
. Example -出现0.0的主要问题是因为你使用//来划分,//将除法截断为最接近的整数,所以以比使用//更大的方式跳过7.0将总是得到0.0。示例 -
>>> 7.0 // 8.0 0.0 >>> 7.0/8.0 0.875
For your case, you should divide using
/
.对于您的情况,您应该使用/来划分。
-
You are taking the mean in each iteration of the loop, though that is not an issue , it may not be completely needed. If you do not want to take the mean in each iteration of the loop, you should indent it outside the loop.
你在循环的每次迭代中取平均值,虽然这不是问题,但可能并不完全需要。如果你不想在循环的每次迭代中取平均值,你应该在循环之外缩进它。
#2
1
You are using //
which means integer division in python 3
你正在使用//这意味着python 3中的整数除法
That is
那是
i.e)
即)
2/4 =0.5
2//4=0
Just replace the //
with /
when calculating r
只需在计算r时用//替换//
#3
0
Here is another approach:
这是另一种方法:
def absolute_difference(values):
last = values[0]
total = 0
for value in values[1:]:
total += abs(value - last)
last = value
return total/(len(values)-1)
print('{:.5f}'.format(absolute_difference([4.0, 4.2, 4.1, 4.4, 4.3])))
Giving the answer: 0.17500
给出答案:0.17500
#4
0
Also, to prevent None
from appearing at the end, you must have return
at the end of your definition. This happens if you make another variable "equal to" (=
) your definition. This was shown in the other posts, but I'm stating this just to highlight things out.
另外,为了防止None出现在最后,您必须在定义的末尾返回。如果您将另一个变量“等于”(=)定义,则会发生这种情况。这在其他帖子中有所体现,但我说这只是为了强调一些事情。