在列表中,具有带有x和y值的坐标。
[(7, 9), (3, 3), (6, 0), (7, 9)]
我可以计算两点之间的距离。
distance_formula = math.sqrt(((x[j]-x[i])**2)+((y[j]-y[i])**2))
我很难遍历列表并计算每个点之间的距离。我想计算第一索引与第二索引,第二索引与第三索引,第三索引与第四索引之间的距离...
您可以用zip()
完成。
coords = [(7, 9), (3, 3), (6, 0), (7, 9)]
for (x1, y1), (x2, y2) in zip(coords, coords[1:]):
distance_formula = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
print(distance_formula)