【发布时间】:2022-11-18 07:11:30
【问题描述】:
对于以下代码段,我似乎遇到了错误,但我不明白“模块”数据类型的来源以及为什么它不可迭代
def find_shortest_path(graph, start, end, shortestLength=-1, path=[]):
path = path + [start]
if start == end:
return path
if start not in graph:
return None
shortest = None
for node in graph[start]:
if node not in path:
if shortestLength == -1 or len(path) < (shortestLength - 1):
newpath = find_shortest_path(graph, node, end, shortestLength, path)
它返回此错误
line 11, in find_shortest_path
if start not in graph:
TypeError: argument of type 'module' is not iterable
【问题讨论】:
-
当您调用
find_shortest_path时,您传递的是模块名称而不是图形作为第一个参数。请发一个minimal reproducible example -
@Barmar 我添加了一些代码以更好地帮助理解错误
-
您仍然没有显示如何调用该函数。原始的
graph值从何而来?
标签: python pandas if-statement