How access different elements of a list for arithmetic operations

python

#1

I am trying to calculate the distance between coordinates (x, y). However I don’t know which loop I should use.
My idea is:

import math as m
station=[]

x = [777610.744,777694.822,778074.698,778546.91,778980.244,779348.78]
y = [8433327.49,8433273.408,8432998.353,8432656.44,8432342.677,8432075.832]

station[0] = mt.sqrt((x[1] - x[0])**2+(y[1] - y[0])**2)
station[1] = mt.sqrt((x[2] - x[1])**2+(y[2] - y[1])**2)
station[2] = mt.sqrt((x[3] - x[2])**2+(y[3] - y[2])**2)
station[3] = mt.sqrt((x[4] - x[3])**2+(y[4] - y[3])**2)
station[4] = mt.sqrt((x[5] - x[4])**2+(y[5] - y[4])**2)

print(station)

Which is not possible …
I’ve tried something with zip function, but I don’t know how to access an element in the next iterative loop:

import math as m
station=[]

x = [777610.744,777694.822,778074.698,778546.91,778980.244,779348.78]
y = [8433327.49,8433273.408,8432998.353,8432656.44,8432342.677,8432075.832]

for i, j in zip(x, y):
    ?????
    station.append(result)

#2

Just playing around and came up with the following…

>>> u = [(x[a + 1] - x[a]) ** 2 for a in range(len(x) - 1)]
>>> v = [(y[a + 1] - y[a]) ** 2 for a in range(len(y) - 1)]
>>> station = [round(sqrt(i + j), 3) for i, j in zip(u, v)]
>>> station
[99.97, 469.0, 583.0, 535.001, 455.0]
>>> 

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.


#4