如何在Python中打印图形中两个相邻点之间的距离

2024-09-28 17:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我用一个文本文件构建了一个图形,打印时它是这样的:

{'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

有人能帮忙吗?你知道吗

谢谢


Tags: innode距离queuenotcurrenttempgraph