如何通过键值删除对象?

How to delete an object by Keyvalue?

提问人:anydel1 提问时间:9/12/2023 更新时间:9/12/2023 访问量:18

问:

我正在使用一个名为 Topologicpy 的 python 软件库。我有 7 个对象。每个人都有一本字典。每个字典都有一个名为“brepStringType”的键,其值为“Face”或“Cell”。我想删除 brepStringType “Cell” 的对象,以便我可以对“Faces”进行进一步的操作。我想概括一下这一点,以便它可以在具有更多面孔和单元格的脚本上工作。

# 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>]
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-3.x 字典 对象 嵌套 键值

评论

0赞 Daviid 9/12/2023
从列表中删除?您可以反向迭代列表,检查 brepTypeString,如果它是 Cell delete,则使用索引。
0赞 Daviid 9/12/2023
或者你可以创建一个非常丑陋的新列表来容纳一行,你只需在你的[vertex for vertex in objectslist if 'Cell' == Dictionary.ValueAtKey(Dictionary.Keys(Topology.Dictionary(vertexObjects)), 'brepTypeString')]for vertexObjects in objectslist:
0赞 anydel1 9/13/2023
@Daviid,感谢您的回复。我尝试了您的单行解决方案: [vertexObjects for o in objectslist if 'Cell' == Dictionary.ValueAtKey(Dictionary.Keys(Topology.Dictionary(vertexObjects)), 'brepTypeString')],但它返回一个空列表 [ ]。不知道为什么。我仍然是一个 python 初学者。我在这里肯定缺少一些关于如何遍历 Topologic.VertexObjects 及其嵌套字典的内容。我能够通过按索引删除“Cell”对象来解决这个问题,但该解决方案不适用于 500 个左右的 Faces 和 Cells 对象。

答: 暂无答案