选择具有给定属性值的网络节点

时间:2015-08-10 14:59:57

标签: python networkx

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

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

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

我想象的东西(这不起作用):

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

2 个答案:

答案 0 :(得分:14)

Python< = 2.7:

根据documentation尝试:

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]

答案 1 :(得分:0)

如果有人被卡住,则提供示例代码

Matrix([[x**2/2], [x**2/2 + x], [x**3/3]])
Matrix([[x, x**2/2, x**3/3], [-x**2/2, 2*x, 0], [-cos(x), sin(x), -log(cos(x))]])
相关问题