我们都知道我们可以将大量数据类型插入到python列表中。例如。人物从列表中删除包含某些数据的对象
X=['a','b','c']
的列表中删除“C”所有我需要做的就是
X.remove('c')
现在我需要的是删除包含特定字符串的对象。
class strng:
ch = ''
i = 0
X = [('a',0),('b',0),('c',0)] #<---- Assume The class is stored like this although it will be actually stored as object references
Object = strng()
Object.ch = 'c'
Object.i = 1
X.remove('c') #<-------- Basically I want to remove the Object containing ch = 'c' only.
# variable i does not play any role in the removal
print (X)
答案我想:
[('a',0),('b',0)] #<---- Again Assume that it can output like this
您正在移除“对象”本身,形成您的列表,因为您的列表中不包含您的对象什么都不会改变。为了获得预期的输出,您需要根据对象的属性删除项目。 – Kasramvd
@Kasramvd是对的。你能描述一下Object.i的作用吗? –
对不起,这是我的错误。我编辑过的代码现在清楚了吗?代码实际上不运行。但是我希望你明白我想要做什么 – XChikuX