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

Python报TypeError: readData()参数不符 两点最短时间计算问题

问题修复方案

1 触发当前报错的直接原因

你调用readData时重复传递了self参数:

# 错误写法
self.readData(self)

Python类的实例方法调用时会自动把实例本身作为第一个self参数传入,手动多传一次self就相当于给仅接受1个参数的readData传了2个参数,直接触发报错。
正确写法只需删掉额外传入的self:

self.readData()

2 代码中其他会导致运行失败的问题

2.1 缺少依赖导入

你调用了math.sqrt、math.pow,但代码未导入math模块,需要在类定义前添加:

import math

2.2 minTime方法存在多处逻辑错误

  • 你写的self.readData()[2]是无效写法:readData没有返回值,调用后得到的是None,对None取下标会触发新报错,直接删掉这行即可
  • 方法内直接使用的line是readData遍历CSV时的临时变量,出了readData作用域就不存在,调用会报变量未定义错误
  • 你当前的storeDistance方法直接把边的[water, 0]属性覆盖成了纯距离,导致后续拿不到路段是否为水域的信息,修改storeDistance保留属性:
def storeDistance(self):
    for entry in self.locationList.items():
        point_info = entry[1]
        neighbours = point_info[1]
        for name, (water, _) in neighbours.items():
            distance = self.computeDistance(point_info[0], self.locationList[name][0])
            # 同时保留路段的水域标记和距离
            neighbours[name] = [water, distance]

2.3 computePathDistance语法错误

你把字典当成函数调用了,要改用方括号取值:

start = self.locationList[path[0]]
end = self.locationList[path[-1]]

2.4 方法名拼写错误

你最初的代码里调用的self.dict,实际定义的方法名是dic,需要统一拼写。

3 修复后的minTime参考实现

你不需要在minTime里重复调用readData,__init__初始化时已经完成了数据读取和距离计算,重复调用是多余操作。时间计算需要按路径的每一段路况分别计算再累加:

def minTime(self, a, b):
    total_time = 0
    # 先获取最短路径
    _, path, _ = self.dic(a,b, [], {}, {}, [])
    # 遍历每一段路径按对应速度算时间
    for i in range(len(path)-1):
        current = path[i]
        next_p = path[i+1]
        water, distance = self.locationList[current][1][next_p]
        speed = 16000 if water else 6000
        total_time += distance / speed
    return int(total_time)

内容的提问来源于stack exchange,提问作者Lilly Mayo-099

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 09:45:09