本文介绍了OOP python-从列表中删除类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表,其中保存了由特定类创建的对象.
I have a list where I save the objects created by a specific class.
我想知道,因为我无法解决这个问题,如何从列表中删除该类的实例?
I would like to know, cause I can't manage to solve this issue, how do I delete an instance of the class from the list?
这应该基于了解对象的一个属性而发生.
This should happen based on knowing one attribute of the object.
推荐答案
遍历列表,找到对象及其位置,然后将其删除:
Iterate through the list, find the object and its position, then delete it:
for i, o in enumerate(obj_list):
if o.attr == known_value:
del obj_list[i]
break
这篇关于OOP python-从列表中删除类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!