1
list = ['02', '03', '04', '05', '06', 'Inactive', 'Inactive', 'Inactive', 'Inactive', 'Inactive']

list.remove('Inactive')

print list

但它只是保持结果不变。我错过了什么?

4

3 回答 3

6

Inactive正如listremove方法的文档所说,它删除了第一次出现的。要删除所有出现,请使用循环/方法/lambda/LC。例如:

myList = ['02', '03', '04', '05', '06', 'Inactive', 'Inactive', 'Inactive', 'Inactive', 'Inactive']
removedList = [x for x in myList if x!='Inactive'] # original list unchanged    
# or
removedList = filter(lambda x: x!='Inactive', myList) #leaves original list intact

顺便说一句,不要list用作变量名

于 2012-12-19T03:58:02.093 回答
0
>>> list = ['02', '03', '04', '05', '06', 'Inactive', 'Inactive', 'Inactive', 'Inactive', 'Inactive']
>>> list.remove('Inactive')
>>> print list
['02', '03', '04', '05', '06', 'Inactive', 'Inactive', 'Inactive', 'Inactive']

它为我删除了一个'Inactive'

于 2012-12-19T04:00:11.953 回答
0
for position, item in enumerate(list):
    list.remove('Inactive')

这将解决问题。

于 2012-12-19T04:01:13.630 回答