Adding edge attribute causes TypeError: 'Atlas

2019-07-28 03:06发布

问题:

Using networkx 2.0 I try to dynamically add an additional edge attribute by looping through all the edges. The graph is a MultiDiGraph.

According to the tutorial it seems to be possible to add edge attributes the way I do in the code below:

g = nx.read_gpickle("../pickles/" + gname)
yearmonth = gname[:7]
g.name = yearmonth  # works
for source, target in g.edges():
    g[source][target]['yearmonth'] = yearmonth

This code throws the following error:

TypeError: 'AtlasView' object does not support item assignment

What am I doing wrong?

回答1:

That should happen if your graph is a nx.MultiGraph. From which case you need an extra index going from 0 to n where n is the number of edges between the two nodes.

Try:

for source, target in g.edges():
    g[source][target][0]['yearmonth'] = yearmonth

The tutorial example is intended for a nx.Graph.



标签: networkx