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

如何按键值删除Topologicpy中brepTypeString为Cell的对象?

问题背景

我正在使用Python库Topologicpy,现有7个对象存储在列表中,每个对象包含一个字典,其中键brepTypeString的值为Face或Cell。需要删除所有brepTypeString为Cell的对象,仅保留Face对象用于后续操作,且代码需通用适配更多同类对象的场景。

现有代码及返回结果

代码1:获取顶点对象列表

# Retrieve the vertex object list using Graph.Vertices
objectslist = Graph.Vertices(g)
print(type(objectslist))
print(objectslist)

返回结果:

<class 'list'>
[<topologic.Vertex object at 0x00000180C8AA69B0>, <topologic.Vertex object at 0x00000180C8AA6B30>, 
<topologic.Vertex object at 0x00000180C8AA74F0>, <topologic.Vertex object at 0x00000180C8AA74B0>, 
<topologic.Vertex object at 0x00000180C8AA7470>, <topologic.Vertex object at 0x00000180C8AA6DB0>, 
<topologic.Vertex object at 0x00000180C8AA5230>]

代码2:遍历对象获取字典信息

for vertexObjects in objectslist: 
    d = Topology.Dictionary(vertexObjects)
    keys = Dictionary.Keys(d)
    brepTypeString = Dictionary.ValueAtKey(d, 'brepTypeString')

    print(vertexObjects)
    print(d)
    print(keys)
    print(keys[2])
    print(brepTypeString)

返回结果:

<topologic.Vertex object at 0x00000180C8AA69B0>
<topologic.Dictionary object at 0x00000180C8F46D70>
['brep', 'brepType', 'brepTypeString', 'location', 'timestamp', 'x', 'y', 'z']
brepTypeString
Face
<topologic.Vertex object at 0x00000180C8AA6B30>
<topologic.Dictionary object at 0x00000180C6E66DF0>
['brep', 'brepType', 'brepTypeString', 'location', 'timestamp', 'x', 'y', 'z']
brepTypeString
Face
<topologic.Vertex object at 0x00000180C8AA74F0>
<topologic.Dictionary object at 0x00000180C8ED66F0>
['brep', 'brepType', 'brepTypeString', 'location', 'timestamp', 'x', 'y', 'z']
brepTypeString
Face
<topologic.Vertex object at 0x00000180C8AA74B0>
<topologic.Dictionary object at 0x00000180C8A99C70>
['brep', 'brepType', 'brepTypeString', 'location', 'timestamp', 'x', 'y', 'z']
brepTypeString
Cell
<topologic.Vertex object at 0x00000180C8AA7470>
<topologic.Dictionary object at 0x00000180C8F4EA30>
['brep', 'brepType', 'brepTypeString', 'location', 'timestamp', 'x', 'y', 'z']
brepTypeString
Face
<topologic.Vertex object at 0x00000180C8AA6DB0>
<topologic.Dictionary object at 0x00000180C6E66DF0>
['brep', 'brepType', 'brepTypeString', 'location', 'timestamp', 'x', 'y', 'z']
brepTypeString
Face
<topologic.Vertex object at 0x00000180C8AA5230>
<topologic.Dictionary object at 0x00000180C8A9A6B0>
['brep', 'brepType', 'brepTypeString', 'location', 'timestamp', 'x', 'y', 'z']
brepTypeString
Face

待解决的核心需求

# How do I delete all objects with key-value brepTypeString "Cell"? 
    if brepTypeString == "Cell": 
        print(brepTypeString)
        # del (????)

解决方案

不要直接在遍历原列表时删除元素(容易引发索引混乱或错误),推荐创建新列表筛选符合条件的对象,这是Python中处理此类需求的通用且安全的方式。

基础实现代码

# 获取原始对象列表
objectslist = Graph.Vertices(g)

# 初始化空列表存储筛选后的Face对象
filtered_faces = []

for obj in objectslist:
    # 获取当前对象的字典
    obj_dict = Topology.Dictionary(obj)
    # 提取brepTypeString的值
    brep_type = Dictionary.ValueAtKey(obj_dict, 'brepTypeString')
    # 只保留Face类型的对象
    if brep_type == "Face":
        filtered_faces.append(obj)

# 后续操作直接使用filtered_faces即可
print(f"筛选后保留的Face对象数量: {len(filtered_faces)}")
print(filtered_faces)

简洁版:列表推导式写法

如果追求代码简洁,可以用列表推导式一步完成筛选,逻辑和上面完全一致:

objectslist = Graph.Vertices(g)
filtered_faces = [
    obj for obj in objectslist
    if Dictionary.ValueAtKey(Topology.Dictionary(obj), 'brepTypeString') == "Face"
]

为什么不直接删除原列表元素?

直接在遍历原列表时用del或list.remove()会导致列表长度动态变化,遍历过程中容易跳过元素或者触发IndexError。而创建新列表的方式逻辑清晰、不易出错,也方便后续调试和维护,完全适配任意数量的Face/Cell对象场景。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:40:59