我试图找到上图的连接组件,如下所示:
connectedcomponentsnew = list(sorted(nx.connected_component_subgraphs(T), key = len, reverse=True))
for G2 in connectedcomponentsnew:
print(G2.edges())
leavesnew = []
for i in G2:
if G2.degree(i) ==1:
leavesnew.append(i)
print('leaveslength', len(leavesnew))
print(leavesnew)
if (len(leavesnew)) == 3:
threeleaves.append(G2)
else:
fourleaves.append(G2)
print('3leaves', threeleaves)
print('4leaves', fourleaves)
代码print('3leaves', threeleaves)
和print('4leaves', fourleaves)
返回以下内容:
3leaves [<networkx.classes.graph.Graph object at 0x1a2349eb70>, <networkx.classes.graph.Graph object at 0x1a215d85c0>, <networkx.classes.graph.Graph object at 0x1a212902e8>, <networkx.classes.graph.Graph object at 0x1a21290fd0>, <networkx.classes.graph.Graph object at 0x1a21290a90>]
4leaves [<networkx.classes.graph.Graph object at 0x1a212901d0>, <networkx.classes.graph.Graph object at 0x1a21290f60>, <networkx.classes.graph.Graph object at 0x1a212909b0>, <networkx.classes.graph.Graph object at 0x1a212907f0>, <networkx.classes.graph.Graph object at 0x1a2167df60>, <networkx.classes.graph.Graph object at 0x1a21290b38>, <networkx.classes.graph.Graph object at 0x1a21290390>, <networkx.classes.graph.Graph object at 0x1a21286f60>, <networkx.classes.graph.Graph object at 0x1a21290eb8>, <networkx.classes.graph.Graph object at 0x1a21290cc0>, <networkx.classes.graph.Graph object at 0x1a21290dd8>, <networkx.classes.graph.Graph object at 0x1a212904e0>, <networkx.classes.graph.Graph object at 0x1a21286e10>, <networkx.classes.graph.Graph object at 0x1a21290be0>, <networkx.classes.graph.Graph object at 0x1a212865f8>, <networkx.classes.graph.Graph object at 0x1a21286d68>, <networkx.classes.graph.Graph object at 0x1a21286978>, <networkx.classes.graph.Graph object at 0x1a212862e8>, <networkx.classes.graph.Graph object at 0x1a21286748>, <networkx.classes.graph.Graph object at 0x1a21286160>, <networkx.classes.graph.Graph object at 0x1a21286358>, <networkx.classes.graph.Graph object at 0x1a212a89e8>, <networkx.classes.graph.Graph object at 0x1a212a8160>, <networkx.classes.graph.Graph object at 0x1a212a8d30>, <networkx.classes.grapmissingh object at 0x1a212a8c18>]
我在代码中遗漏了什么?有人可以帮助我吗?