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

调用geopy.distance.geodesic报int与tuple不支持+=操作如何解决

代码报错修复方案

错误原因

geopy.distance.geodesic()方法要求传入两个独立的坐标元组作为计算参数,而combinations(list_of_coords,2)生成的每一个combo是包含两个坐标元组的嵌套元组,你直接将combo作为单个参数传入方法,不符合入参格式要求,因此触发类型运算错误。另外你原代码缺少依赖导入步骤,未补全的话也会触发找不到方法的报错。

修正后的完整可运行代码

# 导入所需依赖
from itertools import combinations
from geopy.distance import geodesic

list_of_coords = [(5.55, 95.3175), (3.583333, 98.666667), (-0.95556, 100.36056)]
# 用*解包语法将combo里的两个坐标拆成独立参数传入
list_of_distances = [geodesic(*combo).km for combo in combinations(list_of_coords, 2)]

你也可以选择手动拆分参数,写法为geodesic(combo[0], combo[1]).km,运行效果完全一致。运行后生成的list_of_distances会按point1-point2、point1-point3、point2-point3的顺序存储所有两两坐标的公里数距离。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 16:48:04