-1

I have an array that looks something like this [1000, 562, 342, 123, 32, 0] with 0 always being the start point, the values are cumulative distances from 0.

I want to find the average distance between all the points but to get the average distance I need to minus the values in the array to the value next to it. My issue is I'm not sure how to go about getting the minus values since 0 can't minus since it's the last value in the array.

I tried a for loop:

for x in newdata:
    newdata[x] = newdata[x] - newdata[x-1]

but get an error:

TypeError: list indices must be integers or slices, not float

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

The for loop you have wont really work as the x in newdata is accessing the values in the array. Here is my solution:

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)