From 5aeda33275b92e659f1880ee1662c8190740f5f0 Mon Sep 17 00:00:00 2001 From: 0ddoe_s <45603879+0ddoes@users.noreply.github.com> Date: Tue, 23 Aug 2022 21:05:45 +0530 Subject: Updated networkx/classes/function.py . Solves Issue #5463 (#5474) * Updated networkx/classes/function.py * Reformatted using black * Update networkx/classes/function.py Co-authored-by: Ross Barnowski * Update networkx/classes/function.py Co-authored-by: Ross Barnowski * Update networkx/classes/function.py Co-authored-by: Ross Barnowski * Applying style manually Co-authored-by: Ross Barnowski * Apply style manually Co-authored-by: Ross Barnowski * Added example * Updated * Update case of multigraph and case of using G.edges to set H * format doc_string example Co-authored-by: Ross Barnowski Co-authored-by: Dan Schult --- networkx/classes/function.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/networkx/classes/function.py b/networkx/classes/function.py index 7707ec4c..fb24105f 100644 --- a/networkx/classes/function.py +++ b/networkx/classes/function.py @@ -786,6 +786,11 @@ def set_edge_attributes(G, values, name=None): >>> G[1][2]["attr2"] 3 + The attributes of one Graph can be used to set those of another. + + >>> H = nx.path_graph(3) + >>> nx.set_edge_attributes(H, G.edges) + Note that if the dict contains edges that are not in `G`, they are silently ignored:: @@ -794,6 +799,29 @@ def set_edge_attributes(G, values, name=None): >>> (1, 2) in G.edges() False + For multigraphs, the `values` dict is expected to be keyed by 3-tuples + including the edge key:: + + >>> MG = nx.MultiGraph() + >>> edges = [(0, 1), (0, 1)] + >>> MG.add_edges_from(edges) # Returns list of edge keys + [0, 1] + >>> attributes = {(0, 1, 0): {"cost": 21}, (0, 1, 1): {"cost": 7}} + >>> nx.set_edge_attributes(MG, attributes) + >>> MG[0][1][0]["cost"] + 21 + >>> MG[0][1][1]["cost"] + 7 + + If MultiGraph attributes are desired for a Graph, you must convert the 3-tuple + multiedge to a 2-tuple edge and the last multiedge's attribute value will + overwrite the previous values. Continuing from the previous case we get:: + + >>> H = nx.path_graph([0, 1, 2]) + >>> nx.set_edge_attributes(H, {(u, v): ed for u, v, ed in MG.edges.data()}) + >>> nx.get_edge_attributes(H, "cost") + {(0, 1): 7} + """ if name is not None: # `values` does not contain attribute names -- cgit v1.2.1