python networkx在某些情况下删除节点和边缘 [英] python networkx remove nodes and edges with some condition

查看:1295
本文介绍了python networkx在某些情况下删除节点和边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python库networkx中,我想删除具有某些属性的图的节点和边.例如,假设我要删除节点度小于的所有节点和边. 2.考虑以下伪代码:

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 the degree of a node was < 2. Consider the following psuedocode:

vdict = g.degree_dict()         #dictionary of nodes and their degrees
g.remove_from_nodes(v in g s.t. vdict[v] < 2)

我已经看到一些使用集合论符号的语法,但是由于我还是python的新手,所以我不知道如何使用它.如何将其转换为有效的python代码?

I have seen some syntax that uses set theory notation but as I am still new to python I do not know how to use it. How do I convert this into working python code?

推荐答案

如果我们有一个初始化的图g,则以下内容会将f设置为g,但要受每个顶点必须具有度数的约束> 0.我们可以轻松地用一个变量概括0:

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)

这篇关于python networkx在某些情况下删除节点和边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆