python networkx remove nodes and edges with some condition

前端 未结 3 712
-上瘾入骨i
-上瘾入骨i 2021-02-05 22:56

In the python library networkx I would like to remove the nodes and edges of a graph which have some property. For example, suppose I wanted to remove all nodes and edges where

3条回答
  •  星月不相逢
    2021-02-05 23:26

    If we have an initialized graph g the following will set f to be g subject to the constraint that each vertex must have a degree > 0. We could easily generalize 0 with a variable:

    f = nx.Graph()                                                                                                                                     
    fedges = filter(lambda x: g.degree()[x[0]] > 0 and g.degree()[x[1]] > 0, g.edges())
    f.add_edges_from(fedges)
    

提交回复
热议问题