如何遍历一系列坐标并计算它们之间的距离?

问题描述 投票:0回答:1

在列表中,具有带有x和y值的坐标。

[(7, 9), (3, 3), (6, 0), (7, 9)]

我可以计算两点之间的距离。

distance_formula = math.sqrt(((x[j]-x[i])**2)+((y[j]-y[i])**2))

我很难遍历列表并计算每个点之间的距离。我想计算第一索引与第二索引,第二索引与第三索引,第三索引与第四索引之间的距离...

python list iterator coordinates distance
1个回答
0
投票

您可以用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)
© www.soinside.com 2019 - 2024. All rights reserved.