首页 > 解决方案 > 网络中有限分量的平均程度

问题描述

我有一个包含巨大组件和一些有限组件的网络。我需要计算有限分量的平均度数(没有巨型分量)。为此,我尝试从所有组件列表中删除巨型组件并创建控制组件的子图

  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 )  #giant component
  finite_cluster= components.remove(max(components, key=len )) #remove the GC from the components

在这种情况下,错误是

'generator' object has no attribute 'remove' 

那么如何将有限组件与巨型组件分开呢?

标签: pythongraphnetworkx

解决方案


您的基本问题是nx.connected_components返回组件的生成器而不是组件列表。这是 的近亲range。例如,range(10)不返回整数 0-9 的列表;它返回一个迭代器,它将在十次连续调用中返回整数 0-9。

您的修复应该很简单:将生成器输出收集到一个列表中

components = list(nx.connected_components(G))

由于您忽略了发布一个最小的、可重现的示例(MRE),因此我无法为您进行适当的测试。


推荐阅读