You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用geopy.distance的vincenty计算坐标列表距离报错求助

嘿,这个问题我之前也碰到过!咱们来拆解一下:

为什么会报错?

vincenty()函数的设计是接受两个单独的坐标点(每个点是(纬度, 经度)的序列),用来计算两点之间的距离。但你直接传了一个包含三个点的嵌套列表x,函数内部会把整个列表当成单个参数处理,导致后续运算时试图把整数和列表相加,就抛出了TypeError: unsupported operand type(s) for +=: 'int' and 'list'这个错误。

怎么解决?

如果你的需求是计算这组点按顺序连接后的总路径距离,需要遍历相邻的点对,逐个计算距离再累加。这里给你写个可用的代码示例:

import geopy
from geopy.distance import vincenty

x = [[45.4777928, 9.1607807], [45.4625482, 9.1443695], [45.4632698, 9.1977634]]

total_distance = 0.0
# 遍历每一对相邻的坐标点
for i in range(len(x) - 1):
    current_point = x[i]
    next_point = x[i+1]
    # 计算两点间距离,这里用.km获取公里数,也可以用.miles获取英里等
    segment_distance = vincenty(current_point, next_point).km
    total_distance += segment_distance

print(f"总路径距离: {total_distance:.4f} 公里")

额外小提示

如果你的geopy版本是1.14.0及以上,官方已经不再推荐使用vincenty了,建议改用geodesic(精度更高,支持更多坐标系),用法完全一致:

from geopy.distance import geodesic
# 把上面代码里的vincenty替换成geodesic即可

内容的提问来源于stack exchange,提问作者andusci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:56:07