我试图根据节点的程度将网络分成两组(g1和g2)(如果节点d> = 691,则节点在g1中添加,否则节点在g2中添加)
import networkx as nx
import random
import numpy as np
import os
import sys
from networkx.algorithms.assortativity.mixing import degree_mixing_matrix # \attribute_mixing_matrix, numeric_mixing_matrix
from networkx.algorithms.assortativity.pairs import node_degree_xy #, \node_attribute_xy
from operator import itemgetter
from networkx.exception import NetworkXError
import networkx.convert as convert
g = nx.read_edgelist('/home/suman/Desktop/dataset/Email-Enron.txt',create_using=None,nodetype=int,edgetype=int)
s=sorted(g.degree_iter(),key= itemgetter(1),reverse=True)
perc=1
def modify_nw_random(g,perc):
while(perc<=100):
ntm = round((float(perc)/100)*len(g))
#print ntm
while(ntm != 0):
v = random.randrange(1,len(g))
print"node:", v
d=g.degree(v)
print"degree:", d
g1=nx.Graph()
g2=nx.Graph()
if g.has_node(v) and (d>=691):
g1.add_node(v)
else:
g2.add_node(v)
g.remove_node(v)
ntm = ntm-1
if(ntm<=0):break
perc=perc+1
print "perc:",perc
if(perc>100):break
N1=nx.number_of_nodes(g1)
print N1
r2=nx.degree_assortativity_coefficient(g1)
print ("%f"%r2)
N2=nx.number_of_nodes(g2)
print N2
r3=nx.degree_assortativity_coefficient(g2)
print ("%f"%r3)
modify_nw_random(g,perc)
此处第一个循环在perc = 1时执行,然后perc增加1(perc = perc + 1)这不执行。这意味着以下部分代码没有参与执行过程
perc=perc+1
print "perc:",perc
if(perc>100):break
print "node in g1:", g1.nodes()
print "node in g2:", g2.nodes()
N1=nx.number_of_nodes(g1)
print N1
r2=nx.degree_assortativity_coefficient(g1)
print ("%f"%r2)
N2=nx.number_of_nodes(g2)
print N2
r3=nx.degree_assortativity_coefficient(g2)
print ("%f"%r3)
任何人都可以帮助我解决我在这段代码中的错误