问题描述
我有一个包含巨大组件和一些有限组件的网络。我需要计算有限分量(没有巨分量)的平均度数。 为此,我尝试从所有组件的列表中删除巨型组件并创建一个控制组件的子图
components = nx.connected_components(G) #list of all the components
GC = max(components,key=len ) #giant component
finite_cluster= components.remove(max(components,key=len )) #remove the GC from the components
subgraph_finite_cluster= G.subgraph(finite_cluster)
但是我收到子图的错误。
我也尝试删除子图的一部分
components = nx.connected_components(G) #list of all the components
GC = max(components,key=len )) #remove the GC from the components
'generator' object has no attribute 'remove'
那么如何将有限分量与巨型分量分开?
解决方法
您的基本问题是 nx.connected_components
返回组件的生成器,不是组件列表。这是 range
的近亲。例如,range(10)
不返回整数 0-9 的列表;它返回一个迭代器,它将在十次连续调用时返回整数 0-9。
您的修复应该很简单:将生成器输出收集到一个列表中
components = list(nx.connected_components(G))
由于您忽略了发布 minimal,reproducible example (MRE),我无法为您进行测试。
,按长度对组件进行排序并取不包括 GCC 的切片:
finite_cluster = sorted(nx.connected_components(G),key=len)[:-1]
注意:当然,如果您只有一个 GCC,这会起作用。