我用一个文本文件构建了一个图形,打印时它是这样的:
{'Luebeck': [('Hamburg', '63')],
'Hannover': [('Hamburg', '153')],
'Bremen': [('Hamburg', '116')],
'Hamburg': [('Luebeck', '63'), ('Bremen', '116'), ('Hannover', '153')]}
我想使用两个节点之间的距离,比如Luebeck&Hamburg(63)之间的距离,我不知道如何得到这个值。下面是我使用“graph[current][neighbor]”来获取当前和邻居之间的开销的代码。你知道吗
while not queue.empty():
node = queue.get()
current = node[1][len(node[1]) - 1]
if end in node[1]:
print("Path found: ")
break
cost = node[0]
for neighbor in graph[current]:
temp = node[1][:]
temp.append(neighbor)
queue.put((cost + graph[current][neighbor], temp))
显示以下错误:
TypeError: list indices must be integers, not tuple
有人能帮忙吗?你知道吗
谢谢
换了这条线后,工作正常
相关问题 更多 >
编程相关推荐