选择具有给定属性值的网络节点 [英] Select network nodes with a given attribute value

查看:62
本文介绍了选择具有给定属性值的网络节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择并在具有特定属性的图中的节点上执行操作.您将如何选择具有给定属性值的节点?例如:

I'd like to select and perform operations on nodes within a graph with particular attributes. How would you select nodes with a given attribute value? For example:

P=nx.Graph()
P.add_node('node1',at=5)
P.add_node('node2',at=5)
P.add_node('node3',at=6)

有没有办法只选择 at == 5 的节点?

Is there a way to select only the nodes with at == 5?.

我正在想象这样的事情(这行不通):

I'm imagining something like (this doesn't work):

for p in P.nodes():
    P.node[p]['at'==5]

推荐答案

Python <= 2.7:

根据文档 试试:

nodesAt5 = filter(lambda (n, d): d['at'] == 5, P.nodes(data=True))

或者喜欢你的方法

nodesAt5 = []
for (p, d) in P.nodes(data=True):
    if d['at'] == 5:
        nodesAt5.append(p)

Python 2.7 和 3:

nodesAt5 = [x for x,y in P.nodes(data=True) if y['at']==5]

这篇关于选择具有给定属性值的网络节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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