[英]Parsing through edges in NetworkX graph
我试图在不使用 get_edge_attributes() 函数的情况下从图中获取具有特定属性的边。 我需要一种更灵活的方式来做到这一点。 我可以获取节点属性,但由于我是 python 边缘的新手,似乎很难
G = nx.read_graphml("test.graphml")
for n in G:
print "%s\t%s" %(n, G.node[n].get(attr))
for (s,d) in G: # and here is my problem
print "%s->%s\t%s" %(s, d, G.edge[s][d].get(attr))
您可以使用 G.edges() 或 G.edges_iter() 方法循环遍历所有图形边缘。
In [1]: import networkx as nx
In [2]: G = nx.Graph()
In [3]: G.add_edge(1,2,weight=7)
In [4]: G.add_edge(2,3,weight=10)
In [5]: for u,v,a in G.edges(data=True):
print u,v,a
...:
1 2 {'weight': 7}
2 3 {'weight': 10}
接受的答案现在有点过时了: edges_iter
方法已被弃用。
edges
属性可以用作方法或属性。 (其实是一个EdgeView
的值,可迭代,可调用)
现在您可以通过这种方式遍历所有边:
for u,v in G.edges:
print(u,v)
或者像以前一样,使用数据
for u, v, d in G.edges(data=True):
print(f"({u}, {v}) {d=}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.