Steve
Steve

Reputation: 1188

adding vector valued attribute to an edge in networkx

I want to add a list, say [1,2,3] to every edge in a directed networkX graph. Any ideas? I found something like this can be done for nodes (using np.array) but it did not work on edges.

Upvotes: 0

Views: 610

Answers (1)

Joel
Joel

Reputation: 23827

You can add the attributes when you put the edges into the network.

import networkx as nx
G=nx.Graph()
G.add_edge(1,2, values = [1,2,3])
G.add_edge(2,3, values = ['a', 'b', 'c'])
G.edge[1][2]['values']
> [1, 2, 3]
G.edges(data = True)
> [(1, 2, {'values': [1, 2, 3]}), (2, 3, {'values': ['a', 'b', 'c']})]
G.get_edge_data(1,2)
> {'values': [1, 2, 3]}

or after the fact

G.add_edge(4,5)
G.edge[4][5]['values'] = [3.14, 'parrot', 'shopkeeper']
G.edge[5][4]
> {'values': [3.14, 'parrot', 'shopkeeper']}

Upvotes: 2

Related Questions