networkx.set_edge_attributes¶
-
networkx.set_edge_attributes(G, name, values)[source]¶ Sets edge attributes from a given value or dictionary of values.
Parameters: G : NetworkX Graph
name : string
Name of the edge attribute to set.
values : dict
Dictionary of attribute values keyed by edge (tuple). For multigraphs, the tuples must be of the form
(u, v, key), where u and v are nodes and key is the key corresponding to the edge. For non-multigraphs, the keys must be tuples of the form(u, v).If values is not a dictionary, then it is treated as a single attribute value that is then applied to every edge in G. This means that if you provide a mutable object, like a list, updates to that object will be reflected in the edge attribute for each edge.
Examples
After computing some property of the nodes of a graph, you may want to assign a node attribute to store the value of that property for each node:
>>> G = nx.path_graph(3) >>> bb = nx.edge_betweenness_centrality(G, normalized=False) >>> nx.set_edge_attributes(G, 'betweenness', bb) >>> G.edge[1][2]['betweenness'] 2.0
If you provide a list as the third argument, updates to the list will be reflected in the edge attribute for each node:
>>> labels = [] >>> nx.set_edge_attributes(G, 'labels', labels) >>> labels.append('foo') >>> G.edge[0][1]['labels'] ['foo'] >>> G.edge[1][2]['labels'] ['foo']