我想从给定的图形创建具有互斥边缘的另一个图形。Python不是用于搜索列表不工作
我从原始图中随机抽取节点并检查它是否已经存在于原始图中。 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之间。
您期望的结果是什么? –
我正在检查两个图形之间的公共边。答案应该是零。 –
如果你给我一个例子G = nx.karate_club_graph() K = nx.Graph()我会尽力为你取得它,我不认为这很困难,但是很长一段时间我没有使用numpy库,只是阅读代码,我不能帮助对不起! –