networkx.classes.function.set_edge_attributes¶
-
set_edge_attributes
(G, values, name=None)[source]¶ Sets edge attributes from a given value or dictionary of values.
Parameters: G (NetworkX Graph)
values (scalar value, dict-like) – What the edge attribute should be set to. If
values
is not a dictionary, then it is treated as a single attribute value that is then applied to every edge inG
. 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. The attribute name will bename
.If
values
is a dict or a dict of dict, the corresponding edge’ attributes will be updated tovalues
. For multigraphs, the tuples must be of the form(u, v, key)
, whereu
andv
are nodes andkey
is the key corresponding to the edge. For non-multigraphs, the keys must be tuples of the form(u, v)
.name (string (optional, default=None)) – Name of the edge attribute to set if values is a scalar.
Examples
After computing some property of the edges of a graph, you may want to assign a edge attribute to store the value of that property for each edge:
>>> G = nx.path_graph(3) >>> bb = nx.edge_betweenness_centrality(G, normalized=False) >>> nx.set_edge_attributes(G, bb, 'betweenness') >>> G.edges[1, 2]['betweenness'] 2.0
If you provide a list as the second argument, updates to the list will be reflected in the edge attribute for each edge:
>>> labels = [] >>> nx.set_edge_attributes(G, labels, 'labels') >>> labels.append('foo') >>> G.edges[0, 1]['labels'] ['foo'] >>> G.edges[1, 2]['labels'] ['foo']
If you provide a dictionary of dictionaries as the second argument, the entire dictionary will be used to update edge attributes:
>>> G = nx.path_graph(3) >>> attrs = {(0, 1): {'attr1': 20, 'attr2': 'nothing'}, ... (1, 2): {'attr2': 3}} >>> nx.set_edge_attributes(G, attrs) >>> G[0][1]['attr1'] 20 >>> G[0][1]['attr2'] 'nothing' >>> G[1][2]['attr2'] 3