MultiGraph.get_edge_data#
- MultiGraph.get_edge_data(u, v, key=None, default=None)[source]#
Returns the attribute dictionary associated with edge (u, v, key).
If a key is not provided, returns a dictionary mapping edge keys to attribute dictionaries for each edge between u and v.
This is identical to
G[u][v][key]
except the default is returned instead of an exception is the edge doesn’t exist.- Parameters:
- u, vnodes
- defaultany Python object (default=None)
Value to return if the specific edge (u, v, key) is not found, OR if there are no edges between u and v and no key is specified.
- keyhashable identifier, optional (default=None)
Return data only for the edge with specified key, as an attribute dictionary (rather than a dictionary mapping keys to attribute dictionaries).
- Returns:
- edge_dictdictionary
The edge attribute dictionary, OR a dictionary mapping edge keys to attribute dictionaries for each of those edges if no specific key is provided (even if there’s only one edge between u and v).
Examples
>>> G = nx.MultiGraph() # or MultiDiGraph >>> key = G.add_edge(0, 1, key="a", weight=7) >>> G[0][1]["a"] # key='a' {'weight': 7} >>> G.edges[0, 1, "a"] # key='a' {'weight': 7}
Warning: we protect the graph data structure by making
G.edges
andG[1][2]
read-only dict-like structures. However, you can assign values to attributes in e.g.G.edges[1, 2, 'a']
orG[1][2]['a']
using an additional bracket as shown next. You need to specify all edge info to assign to the edge data associated with an edge.>>> G[0][1]["a"]["weight"] = 10 >>> G.edges[0, 1, "a"]["weight"] = 10 >>> G[0][1]["a"]["weight"] 10 >>> G.edges[1, 0, "a"]["weight"] 10
>>> G = nx.MultiGraph() # or MultiDiGraph >>> nx.add_path(G, [0, 1, 2, 3]) >>> G.edges[0, 1, 0]["weight"] = 5 >>> G.get_edge_data(0, 1) {0: {'weight': 5}} >>> e = (0, 1) >>> G.get_edge_data(*e) # tuple form {0: {'weight': 5}} >>> G.get_edge_data(3, 0) # edge not in graph, returns None >>> G.get_edge_data(3, 0, default=0) # edge not in graph, return default 0 >>> G.get_edge_data(1, 0, 0) # specific key gives back {'weight': 5}