Given any graph G created in NetworkX, I want to be able to assign some weights to G.edges() after the graph is created. The graphs involved are grids, erdos-reyni, barabasi-albert, and so forth.
Given my G.edges()
:
[(0, 1), (0, 10), (1, 11), (1, 2), (2, 3), (2, 12), ...]
And my weights
:
{(0,1):1.0, (0,10):1.0, (1,2):1.0, (1,11):1.0, (2,3):1.0, (2,12):1.0, ...}
How can I assign each edge the relevant weight? In this trivial case all weights are 1.
I've tried to add the weights to G.edges() directly like this
for i, edge in enumerate(G.edges()):
G.edges[i]['weight']=weights[edge]
But I get this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-6119dc6b7af0> in <module>()
10
11 for i, edge in enumerate(G.edges()):
---> 12 G.edges[i]['weight']=weights[edge]
TypeError: 'instancemethod' object has no attribute '__getitem__'
What's wrong? Since G.edges()
is a list, why can't I access its elements as with any other list?