list = ['02', '03', '04', '05', '06', 'Inactive', 'Inactive', 'Inactive', 'Inactive', 'Inactive']
list.remove('Inactive')
print list
但它只是保持结果不变。我错过了什么?
答案 0 :(得分:6)
它删除了Inactive
的第一次出现documentation of list remove
method。要删除所有匹配项,请使用loop / method / 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
用作变量名称
答案 1 :(得分: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'
。
答案 2 :(得分:0)
for position, item in enumerate(list):
list.remove('Inactive')
这可以解决问题。