summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author0ddoe_s <45603879+0ddoes@users.noreply.github.com>2022-08-23 21:05:45 +0530
committerJarrod Millman <jarrod.millman@gmail.com>2022-09-30 09:49:29 -0700
commit5aeda33275b92e659f1880ee1662c8190740f5f0 (patch)
treeaac5e708ff501cbcad4c3894207b80a9bfb772fb
parenta8a320b9a1126f8ce6590e3f320264c6d2b06249 (diff)
downloadnetworkx-5aeda33275b92e659f1880ee1662c8190740f5f0.tar.gz
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 <rossbar@berkeley.edu> * Update networkx/classes/function.py Co-authored-by: Ross Barnowski <rossbar@berkeley.edu> * Update networkx/classes/function.py Co-authored-by: Ross Barnowski <rossbar@berkeley.edu> * Applying style manually Co-authored-by: Ross Barnowski <rossbar@berkeley.edu> * Apply style manually Co-authored-by: Ross Barnowski <rossbar@berkeley.edu> * 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 <rossbar@berkeley.edu> Co-authored-by: Dan Schult <dschult@colgate.edu>
-rw-r--r--networkx/classes/function.py28
1 files changed, 28 insertions, 0 deletions
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