Reputation: 1521
I'm trying to print the edge attributes of a Networkx graph in a sorted manner.
For instance,
print(sorted(G.edges(data=True))
will display an ordered dict
[(3, 7, OrderedDict([('w1', 9.62), ('w2', 37.2)])), (3, 8, OrderedDict([('w1', 9.42), ('w2', 49.6)]))]
Likewise, I want to print a single attribute ( only w1
or w2
, data=True prints both) as a sorted output.
Example, when I try
print(sorted(nx.get_edge_attributes(G, 'w1').values()))
doesn't work.
Any suggestions on how to display sorted output of a single attribute will be really helpful.
Upvotes: 0
Views: 470
Reputation: 88226
nx.get_edge_attributes
will only return the actual attrubutes. Probably the simples way could be to keep only one of the attributes from the result of G.edges(data=True)
:
G = nx.Graph()
G.add_edge(3, 8, w1= 9.62, w2=37.2)
G.add_edge(3, 7, w1= 9.42, w2=49.6)
attr = 'w1'
sorted(((*edge, (attr, d[attr])) for *edge, d in G.edges(data=True)))
# [(3, 7, ('w1', 9.42)), (3, 8, ('w1', 9.62))]
Upvotes: 1