【发布时间】:2016-02-27 07:49:15
【问题描述】:
我正在尝试分别读取具有源目标权重值的文件,以找到最短路径并使用最终权重打印该路径。我正在使用 Networkx 来使用 Dijkstra 函数。但是,当我执行时,我会收到此通知:<function shortest_path at 0x7f9a13c39230>
<function bidirectional_dijkstra at 0x7f9a13c48140>
我最初只使用了shortest_path 函数,以为我在使用它时搞砸了,因此使用了bidirectional_dijkstra 函数。
有问题的文件包含名为 example.txt 的虚拟数据:
D1 D5 7
D1 D2 6
D5 D4 7
D5 D3 7
D5 D3 3
D5 D2 4
D2 D2 1
D4 D3 1
我在 Python 中的方法:
#!/usr/bin/env python
import os.path
import networkx as nx
from sys import argv
#!script, filename = argv
#assigns example to open the file example.txt
example = open("example.txt", "r")
print("\n This is what we have in our file:\n")
#prints the open file
print example.read()
G =nx.Graph()
for line in example:
source, target, weight = line.split()
nx.shortest_path(G,[source, target, weight])
print ( nx.shortest_path)
print ( nx.bidirectional_dijkstra)
#for some reason, this is printing:
#
#<function shortest_path at 0x7fe480d56230>
#<function bidirectional_dijkstra at 0x7fe480d65140>
【问题讨论】:
-
您实际上并没有调用该函数。即
nx.shortest_path是实际函数,nx.shortest_path()会给你返回值。 -
那么,我应该将
nx.shortest_path更改为nx.shortest_path()? -
那么你想打印什么?看起来你在 while 循环中正确调用了它,但没有捕获返回值
-
我知道出了什么问题;我没有定义边缘。我已将边缘添加到图表中,它现在可以工作了。谢谢!我得到了我需要的结果。
-
我没做什么,但还好。确保将您的修复作为答案发布,以便其他人看到问题所在:)