Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant access to edge attributes in networkx

Is it indeed the case that to access edge attributes in networkx the awkward third form below is necessary and no variation of the more svelte first two forms will do?

import networkx as nx

G = nx.Graph()
G.add_edge(1, 2, weight=4.7 )
G.add_edge(3, 4, weight=5.8 )

# for edge in G.edges():
#     print edge['weight']
# 
# for edge in G.edges():
#     print G[edge]['weight']

for edge in G.edges():
    print G.edge[edge[0]][edge[1]]['weight']
like image 962
Calaf Avatar asked Feb 04 '16 00:02

Calaf


1 Answers

Use data=True:

import networkx as nx

G = nx.Graph()
G.add_edge(1, 2, weight=4.7)
G.add_edge(3, 4, weight=5.8)

for node1, node2, data in G.edges(data=True):
    print(data['weight'])

prints

4.7
5.8
like image 87
unutbu Avatar answered Sep 19 '22 16:09

unutbu