MultiGraph.edges#
- property MultiGraph.edges#
Returns an iterator over the edges.
edges(self, nbunch=None, data=False, keys=False, default=None)
The MultiEdgeView provides set-like operations on the edge-tuples as well as edge attribute lookup. When called, it also provides an EdgeDataView object which allows control of access to edge attributes (but does not provide set-like operations). Hence,
G.edges[u, v, k]['color']
provides the value of the color attribute for the edge fromu
tov
with keyk
whilefor (u, v, k, c) in G.edges(data='color', keys=True, default="red"):
iterates through all the edges yielding the color attribute with default'red'
if no color attribute exists.Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data). If
keys=True
is not provided, the tuples will just be (node, neighbor, data), but multiple tuples with the same node and neighbor will be generated when multiple edges exist between two nodes.- Parameters:
- nbunchsingle node, container, or all nodes (default= all nodes)
The view will only report edges from these nodes.
- datastring or bool, optional (default=False)
The edge attribute returned in 3-tuple (u, v, ddict[data]). If True, return edge attribute dict in 3-tuple (u, v, ddict). If False, return 2-tuple (u, v).
- keysbool, optional (default=False)
If True, return edge keys with each edge, creating (u, v, k) tuples or (u, v, k, d) tuples if data is also requested.
- defaultvalue, optional (default=None)
Value used for edges that don’t have the requested attribute. Only relevant if data is not True or False.
- Returns:
- edgesMultiEdgeView
A view of edge attributes, usually it iterates over (u, v) (u, v, k) or (u, v, k, d) tuples of edges, but can also be used for attribute lookup as
edges[u, v, k]['foo']
.
Notes
Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.
Examples
>>> G = nx.MultiGraph() >>> nx.add_path(G, [0, 1, 2]) >>> key = G.add_edge(2, 3, weight=5) >>> key2 = G.add_edge(2, 1, weight=2) # multi-edge >>> [e for e in G.edges()] [(0, 1), (1, 2), (1, 2), (2, 3)] >>> G.edges.data() # default data is {} (empty dict) MultiEdgeDataView([(0, 1, {}), (1, 2, {}), (1, 2, {'weight': 2}), (2, 3, {'weight': 5})]) >>> G.edges.data("weight", default=1) MultiEdgeDataView([(0, 1, 1), (1, 2, 1), (1, 2, 2), (2, 3, 5)]) >>> G.edges(keys=True) # default keys are integers MultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 1), (2, 3, 0)]) >>> G.edges.data(keys=True) MultiEdgeDataView([(0, 1, 0, {}), (1, 2, 0, {}), (1, 2, 1, {'weight': 2}), (2, 3, 0, {'weight': 5})]) >>> G.edges.data("weight", default=1, keys=True) MultiEdgeDataView([(0, 1, 0, 1), (1, 2, 0, 1), (1, 2, 1, 2), (2, 3, 0, 5)]) >>> G.edges([0, 3]) # Note ordering of tuples from listed sources MultiEdgeDataView([(0, 1), (3, 2)]) >>> G.edges([0, 3, 2, 1]) # Note ordering of tuples MultiEdgeDataView([(0, 1), (3, 2), (2, 1), (2, 1)]) >>> G.edges(0) MultiEdgeDataView([(0, 1)])