List 基于for-loop的网络节点删除
我想使用networkx删除图的节点,并在每次迭代时存储中心性度量。但是我得到了这个错误“字典在迭代过程中改变了大小” 以下是我的尝试:List 基于for-loop的网络节点删除,list,loops,dictionary,iteration,List,Loops,Dictionary,Iteration,我想使用networkx删除图的节点,并在每次迭代时存储中心性度量。但是我得到了这个错误“字典在迭代过程中改变了大小” 以下是我的尝试: import copy GDC = G.copy() import random for xn in GDC.nodes(): RndmSmpl = random.sample(GDC.nodes, 1) print(RndmSmpl) GDC.remove_nodes_from(RndmSmpl) deg = dict(G
import copy
GDC = G.copy()
import random
for xn in GDC.nodes():
RndmSmpl = random.sample(GDC.nodes, 1)
print(RndmSmpl)
GDC.remove_nodes_from(RndmSmpl)
deg = dict(GDC.degree())
nx.set_node_attributes(GDC,deg, 'degree')
cc = nx.closeness_centrality(GDC)
nx.set_node_attributes(GDC , cc , 'Clsns Cntr')
t = pd.DataFrame.from_dict(dict(GDC.nodes(data=True)), orient = 'index')
t.sort_values('degree', ascending = False)
total_deg = t.loc[:, 'degree'].sum()
total_cc = t.loc[:, 'Clsns Cntr'].sum()
total_centrality = [len(GDC.nodes()), total_deg , total_dc , total_cc]
print(total_centrality)