networkx.classes.reportviews.DegreeView#
- class DegreeView(G, nbunch=None, weight=None)[source]#
A DegreeView class to act as G.degree for a NetworkX Graph
Typical usage focuses on iteration over (node, degree) pairs. The degree is by default the number of edges incident to the node. Optional argument weight enables weighted degree using the edge attribute named in the weight argument. Reporting and iteration can also be restricted to a subset of nodes using nbunch.
Additional functionality include node lookup so that G.degree[n] reported the (possibly weighted) degree of node n. Calling the view creates a view with different arguments nbunch or weight.
- Parameters:
- graphNetworkX graph-like class
- nbunchnode, container of nodes, or None meaning all nodes (default=None)
- weightstring or None (default=None)
Notes
DegreeView can still lookup any node even if nbunch is specified.
Examples
>>> G = nx.path_graph(3) >>> DV = G.degree() >>> assert DV[2] == 1 >>> assert G.degree[2] == 1 >>> assert sum(deg for n, deg in DV) == 4
>>> DVweight = G.degree(weight="span") >>> G.add_edge(1, 2, span=34) >>> DVweight[2] 34 >>> DVweight[0] # default edge weight is 1 1 >>> sum(span for n, span in DVweight) # sum weighted degrees 70
>>> DVnbunch = G.degree(nbunch=(1, 2)) >>> assert len(list(DVnbunch)) == 2 # iteration over nbunch only
- __init__(G, nbunch=None, weight=None)#
Methods