Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 给定一个点数组,如何获得数组中所有点之间的距离_Python_Arrays - Fatal编程技术网

Python 给定一个点数组,如何获得数组中所有点之间的距离

Python 给定一个点数组,如何获得数组中所有点之间的距离,python,arrays,Python,Arrays,我有一个数组,看起来像这样[1000562342123,32,0],其中0始终是起点,值是从0开始的累积距离 我想找到所有点之间的平均距离,但要得到平均距离,我需要将数组中的值减去它旁边的值。我的问题是我不确定如何获得负值,因为0不能减去,因为它是数组中的最后一个值 我尝试了for循环: 新数据中x的: newdata[x]=newdata[x]-newdata[x-1] 但是得到一个错误: TypeError:列表索引必须是整数或片,而不是浮点 由于newdata中的x正在访问数组中的值,因

我有一个数组,看起来像这样
[1000562342123,32,0]
,其中
0
始终是起点,值是从0开始的累积距离

我想找到所有点之间的平均距离,但要得到平均距离,我需要将数组中的值减去它旁边的值。我的问题是我不确定如何获得负值,因为0不能减去,因为它是数组中的最后一个值

我尝试了
for
循环:

新数据中x的
:
newdata[x]=newdata[x]-newdata[x-1]
但是得到一个错误:

TypeError:列表索引必须是整数或片,而不是浮点


由于newdata中的
x正在访问数组中的值,因此您使用的
for
循环实际上无法工作。以下是我的解决方案:

data =  [1000,562,342,123,32,0]
distances = [] 
avgDist = 0

# compute distances between points
for i in range(len(data) - 1):
  dist = data[i] - data[i+1]
  distances.append(dist) 

# get the average
avgDist = sum(distances)/len(distances)

您必须遍历由列表长度定义的范围。这是数组还是列表?它看起来像一个列表。这种区别在python中非常重要。