目录
1.调用StringIO
2021.8.26
import networkx as nx
import StringIO
edges = StringIO.StringIO("""
a b 1
a bb 1
b c 2
bb c 2
c d 1
a d 10""")
G = nx.read_weighted_edgelist(edges, nodetype=str)
print (list(nx.all_shortest_paths(G, 'a', 'd', weight='weight')))
# [['a', 'b', 'c', 'd'], ['a', 'bb', 'c', 'd']]
报错:ModuleNotFoundError: No module named ‘StringIO’
解决方式:Python3中已将StringIO归入io,调用方法如下:
import io
iost = io.StringIO()
完整代码:
import networkx as nx
import io
edges = io.StringIO("""
a b 1
a bb 1
b c 2
bb c 2
c d 1
a d 10""")
G = nx.read_weighted_edgelist(edges, nodetype=str)
print (list(nx.all_shortest_paths(G, 'a', 'd', weight='weight')))
# [['a', 'b', 'c', 'd'], ['a', 'bb', 'c', 'd']]
代码作用:一个简单的例子来说明all_shortest_paths()是如何工作的
带有networkx的加权图的所有最短路径?