【问题标题】:why do i get TypeError: argument of type 'module' is not iterable为什么我得到 TypeError: argument of type \'module\' is not iterable
【发布时间】: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


【解决方案1】:

根据这个问题: finding the shortest path in a graph from start to end vertex

你可能会使用:

if not graph.has_key(start):

代替

if start not in graph:

【讨论】:

    猜你喜欢
    • 2022-12-04
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-27
    • 2022-12-27
    • 2023-01-12
    相关资源
    最近更新 更多