我试图从给定的图中创建另一个图,它具有相互排斥的边。
我正在随机地从原始图中绘制节点,并检查它是否已经存在于原始图中。not in命令不像预期的那样工作。
我的代码如下。
import networkx as nx
import numpy as np
import random
G=nx.karate_club_graph()
K=nx.Graph()
sample_len=len(G.edges())
while(len(K.edges())<sample_len):
n1=random.choice(G.nodes())
n2=random.choice(G.nodes())
e=(n1,n2)
if(e not in G.edges()):
K.add_edge(*e)
i=0
for x in G.edges():
if(x in K.edges()):
i+=1
print i每次运行这段代码时,i的值都在5-10之间。
发布于 2017-04-24 14:10:48
如果您想要这样做,您可以这样做:
print (list(set(G.edges()) - set(K.edges())))如果你只想要一个数字,加入连
print (len(list(set(G.edges()) - set(K.edges()))))如果这是你想要存档的,请告诉我;)
https://stackoverflow.com/questions/43586021
复制相似问题