0

I'm using the NetworkX module on Python 3.2.3. In a multigraph G with multiple edges between two nodes - say, 'a' and 'b' with three edges between them - typing G['a'].keys() into the IDLE prompt returns a dict_keys list with 'b' occurring only once in it. Any way to make it so that 'b' occurs as many times as there are edges between the two nodes?

4

2 に答える 2

1

Something like

[(k, len(v)) for k, v in G['a'].items()]
于 2012-07-05T14:33:52.500 に答える
1

The NetworkX API suggests you use G.neighbors(), but for multigraphs that will only give the unique neighbors.

If you want all neighbors you can use [v for u,v in G.edges_iter()], e.g.

In [1]: import networkx as nx

In [2]: G=nx.MultiGraph()

In [3]: G.add_edge('a','b')

In [4]: G.add_edge('a','b')

In [5]: G.add_edge('a','b')

In [6]: [v for u,v in G.edges_iter()]
Out[6]: ['b', 'b', 'b']
于 2012-07-06T18:20:45.607 に答える