Python 将networkx与我自己的对象一起使用

Python 将networkx与我自己的对象一起使用,python,graph-theory,networkx,Python,Graph Theory,Networkx,我有自己的东西,比如意大利香肠。我有一份每一个意大利香肠的边缘列表和一份意大利香肠列表。然后,我使用networkx构建了一个图形。我想找出从一个意大利香肠到另一个意大利香肠的最短路径的重量。但是,我得到一个错误,如下所示,它从networkx跟踪内部内容,如下所示: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pizza.py", line 437, i

我有自己的东西,比如意大利香肠。我有一份每一个意大利香肠的边缘列表和一份意大利香肠列表。然后,我使用networkx构建了一个图形。我想找出从一个意大利香肠到另一个意大利香肠的最短路径的重量。但是,我得到一个错误,如下所示,它从networkx跟踪内部内容,如下所示:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
  File "pizza.py", line 437, in shortestPath
    cost = nx.shortest_path_length(a, spepp, tpepp, True)
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest_paths/generic.py", line 181, in shortest_path_length
    paths=nx.dijkstra_path_length(G,source,target)
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest_paths/weighted.py", line 119, in dijkstra_path_length
    (length,path)=single_source_dijkstra(G,source, weight = weight)
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest_paths/weighted.py", line 424, in single_source_dijkstra
    edata=iter(G[v].items())
  File "/Library/Python/2.6/site-packages/networkx-1.3-py2.6.egg/networkx/classes/graph.py", line 323, in __getitem__
    return self.adj[n]
KeyError: <pizza.pepperoni object at 0x100ea2810>
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“pizza.py”,第437行,在最短路径中
成本=nx.最短路径长度(a、spepp、tpepp、True)
文件“/Library/Python/2.6/site packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest\u paths/generic.py”,第181行,最短路径长度
路径=nx.dijkstra\u路径长度(G,源,目标)
文件“/Library/Python/2.6/site packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest_paths/weighted.py”,第119行,dijkstra_路径长度
(长度,路径)=单源dijkstra(G,源,重量=重量)
文件“/Library/Python/2.6/site packages/networkx-1.3-py2.6.egg/networkx/algorithms/shortest\u paths/weighted.py”,第424行,在single\u source\u dijkstra中
edata=iter(G[v].items())
文件“/Library/Python/2.6/site packages/networkx-1.3-py2.6.egg/networkx/classes/graph.py”,第323行,在__
返回自我
关键错误:
你知道错误是什么吗?或者我必须在我的pizza类中添加什么才能避免这个关键错误


编辑:我的边格式正确。我不知道这些对象是否可以作为节点处理。

如果将每个边和节点都作为一个列表,那么在networkx中构建图形就很简单了。鉴于在构建图形对象时出现问题,最好的诊断方法可能是在networkx中逐步完成图形构建:

import networkx as NX
import string
import random

G = NX.Graph()    # initialize the graph

# just generate some synthetic data for the nodes and edges:
my_nodes = [ ch for ch in string.ascii_uppercase ]
my_nodes2 = list(my_nodes)
random.shuffle(my_nodes2)
my_edges = [ t for t in zip(my_nodes, my_nodes2) if not t[0]==t[1] ]

# now add the edges and nodes to the networkx graph object:
G.add_nodes_from(my_nodes)
G.add_edges_from(my_edges)

# look at the graph's properties:
In [87]: len(G.nodes())
Out[87]: 26

In [88]: len(G.edges())
Out[88]: 25

In [89]: G.edges()[:5]
Out[89]: [('A', 'O'), ('A', 'W'), ('C', 'U'), ('C', 'F'), ('B', 'L')]

# likewise, shortest path calculation is straightforward
In [86]: NX.shortest_path(G, source='A', target='D', weighted=False)
Out[86]: ['A', 'W', 'R', 'D']
根据我的经验,Networkx有一个非常宽松的接口,特别是它可以接受广泛的对象类型作为节点和边。节点可以是除None之外的任何可哈希对象


我能想到的唯一一件可能会导致您在Q中出现错误的事情是,在您装箱图形之后,您可能直接操纵了图形对象(dict,*G*),这是您不应该做的--有很多访问器方法

老实说,我不知道我的问题到底是什么,但我和这个对象鬼混了一下,终于让它正常工作了。感谢您深思熟虑的回复。这让我想到:)