Adding edge attribute causes TypeError: 'Atlas

2019-07-28 03:23发布

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?

标签: networkx
1条回答
手持菜刀,她持情操
2楼-- · 2019-07-28 03:49

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.

查看更多
登录 后发表回答