简体   繁体   中英

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.

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']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM