Home

Published

- 1 min read

networkx remove nodes with degree

img of networkx remove nodes with degree

The solution for this is noted below

networkx remove nodes with degree

Solution

   import networkx

G1 = networkx.Graph()
G1.add_edges_from([('a','b'),('b','c'),('c','e'),('c','d'),('c','f'),('c','g')])
to_be_removed = [x for  x in G1.nodes() if G1.degree(x) <= 1]

for x in to_be_removed:
    G1.remove_node(x)

print(G1.edges())

Try other methods by searching on the site. That is if this doesn’t work