You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetworkX图中过滤非关联节点:保留红-绿-蓝/红节点连接需求

链式网络图节点过滤问题

我有一个节点呈链式连接的网络图,此前通过「判断节点仅含一个邻居」的逻辑过滤节点,但仍有不符合要求的节点残留(详见附图)。需要仅保留以下两种连接结构:

  • 红色节点通过绿色节点连接至蓝色节点
  • 红色节点通过绿色节点连接至另一个红色节点

所用数据见附图,当前实现代码如下:

for u, v in CA_new.edges:
    
    if len(list(CA_new.neighbors(v))) == 1 and len(list(CA_new.neighbors(u))) == 1:
        removeNodeslist.append(v)
        removeNodeslist.append(u)
    else:
        keepNodeslist.append(v)
        keepNodeslist.append(u)
CA_new.remove_nodes_from(removeNodeslist)

内容的提问来源于stack exchange,提问作者john_sab1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:10:42