Delete edge attributes

2,435 views
Skip to first unread message

GDG

unread,
Dec 18, 2013, 11:53:10 AM12/18/13
to networkx...@googlegroups.com
Hi, with networkx is it possible to delete attributes of edges? Something like
del G.edge[v1][v2][a]

where a is the attribute to be deleted?

Thanks,
G.

Dan Schult

unread,
Dec 18, 2013, 2:27:22 PM12/18/13
to networkx...@googlegroups.com
Yes, that should work. Also
del G[v1][v2][a]

Be careful with multigraphs though. You need the key of the edge you want to change the attributes for:
del MG[v1][v2][key][a]
> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
> To post to this group, send email to networkx...@googlegroups.com.
> Visit this group at http://groups.google.com/group/networkx-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.

GDG

unread,
Dec 18, 2013, 3:21:57 PM12/18/13
to networkx...@googlegroups.com
I am getting this error:

Traceback (most recent call last):
  File "../scripts/yed2graphml.py", line 27, in <module>
    del G.edge['n0']['n1']['iteration']
KeyError: 'iteration'

The nodes are 'n0' and 'n1' ; the attributes is 'iteration'.

I am reading the graph from a graphml file (I assume it a multigraph).

G = nx.read_graphml(sys.argv[1])

If I do:

del G.edge['n0']['n1'][0]['iteration']

I get a similar error

Traceback (most recent call last):
  File "../scripts/yed2graphml.py", line 27, in <module>
    del G.edge['n0']['n1'][0]['iteration']
KeyError: 0

Do you have any suggestion?

Dan Schult

unread,
Dec 18, 2013, 3:30:12 PM12/18/13
to networkx...@googlegroups.com
You should print the attributes to see what they really are (as opposed to what you think they are):
print G.edge['n0']['n1']

GDG

unread,
Dec 18, 2013, 3:50:54 PM12/18/13
to networkx...@googlegroups.com
Yes. Fixed.

Is there a way to automatically check if an attributes exist for an edge and only in that case proceed with the attribute removal?

Dan Schult

unread,
Dec 18, 2013, 3:54:08 PM12/18/13
to networkx...@googlegroups.com
attr=G['n0']['n1']
if a in attr:
del attr[a]

GDG

unread,
Dec 18, 2013, 3:58:24 PM12/18/13
to networkx...@googlegroups.com
Thanks!

GDG

unread,
Dec 18, 2013, 7:53:00 PM12/18/13
to networkx...@googlegroups.com
I hope that the following will be the last question :P

I am reading my graph from GraphML, the resulting graph can either be or not be a multigraph. In the first case,  I have to use the keys for deleting attributes etc.

G = nx.read_graphml(sys.argv[1])

edge_attributes = ['id', 'iteration']
for ae in edge_attributes:
  for (u, v, k) in G.edges(keys=True):
    ae_list = G.edge[u][v][k]
    if ae in ae_list:
      del G[u][v][k][ae]

If the graph is not a multigraph, the previous code gives an error:

   for (u, v, k) in G.edges(keys=True):
TypeError: edges() got an unexpected keyword argument 'keys'

How can I check if the current graph is or is not a multigraph?

Aric Hagberg

unread,
Dec 18, 2013, 8:42:59 PM12/18/13
to networkx...@googlegroups.com
You can use G.is_multigraph().
Aric
Reply all
Reply to author
Forward
0 new messages