I have a networkx digraph with attributes on the nodes, and I want to find all edges where a particular node attribute is different. Is there a way to do this automatically, or do I have to iterate with edge_iter(data=True)
and find them myself?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
There is no built-in function for this but it is pretty simple:
import networkx as nx
G = nx.Graph()
G.add_node(1,color='red')
G.add_node(2,color='red')
G.add_node(3,color='blue')
G.add_node(4,color='blue')
G.add_edges_from([(1,2),(1,3),(3,4)])
for (u,v) in G.edges_iter():
if G.node[u]['color'] != G.node[v]['color']:
print u,v