我无法将生成器(nx.connected_components)转换为列表

时间:2019-05-13 03:11:18

标签: python

我已将34个节点的图分成2个子图。我希望使用list命令来转换两个节点列表。

com=list(nx.connected_components(K2))

但是输出不是两个列表。 com的输出为

[(0, None), (1, None), (2, None), (3, None), (4, None), (5, None), (6, None), (7, None), (8, None), (9, None), (10, None), (11, None), (12, None), (13, None), (14, None), (15, None), (16, None), (17, None), (18, None), (19, None), (20, None), (21, None), (22, None), (23, None), (24, None), (25, None), (26, None), (27, None), (28, None), (29, None), (30, None), (31, None), (32, None), (33, None)]

我之后

print(com[0])

不返回任何内容。

我试图将其转换为数据框。但这很麻烦。

K=nx.karate_blub_graph()
def remove_top_eb(KK):
eb= nx.edge_betweenness_centrality(KK)
sorted_eb = sorted(eb.items(), key=lambda x: x[1], reverse=True)
edge_to_remove = sorted_eb[0]#tuple
KK.remove_edge(*edge_to_remove[0])#* is to unpack tuple into two nodes. 
return KK
K2= K.copy()#make a copy of the original graph
for i in range(12):
K2=remove_top_eb(K2) 
print("We have ", nx.number_connected_components(K2), " components")
nx.draw(K2, pos)
nx.draw_networkx_labels(K2, pos)
plt.show()com=list(nx.connected_components(K2))
print(com)
print(com[0])

0 个答案:

没有答案