python - 网络中有限分量的平均程度
问题描述
我有一个包含巨大组件和一些有限组件的网络。我需要计算有限分量的平均度数(没有巨型分量)。为此,我尝试从所有组件列表中删除巨型组件并创建控制组件的子图
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'
那么如何将有限组件与巨型组件分开呢?
解决方案
您的基本问题是nx.connected_components
返回组件的生成器,而不是组件列表。这是 的近亲range
。例如,range(10)
不返回整数 0-9 的列表;它返回一个迭代器,它将在十次连续调用中返回整数 0-9。
您的修复应该很简单:将生成器输出收集到一个列表中
components = list(nx.connected_components(G))
由于您忽略了发布一个最小的、可重现的示例(MRE),因此我无法为您进行适当的测试。
推荐阅读
- angular - Nativescript SDK 错误
- python - 如何使用 django 的 ORM 进行两个连接
- firebase - Expo Snack:_firebase.default.firestore 不是函数
- python-3.x - 基本 Flask SQLAlchemy 上下文问题
- c - 以字符串文字作为参数的 strtok
- regex - MongoDB $regex 查询号码失败
- chart.js - 如何去除彩色标签方块
- go - graphql-go 将字段类型定义为对象
- google-sheets - 比较两行中的每个单元格,但有一个例外
- dynamics-crm - MS Dynamics CRM 365 - 从 Excel 文件导入数据