-1
我试图读取一个文件与源目标体重值分别找到最短的路径和打印与最终重量的路径。我使用Networkx来使用Dijkstra函数。然而,当我执行,我收到此通知:
<function shortest_path at 0x7f9a13c39230> <function bidirectional_dijkstra at 0x7f9a13c48140>
Dijkstra的算法与Networkx的实现,显示打印错误
我最初使用只是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()'? – Atychip
那么你想要打印什么?看起来像你在while循环中正确调用它,但没有捕获返回值 –