To remove an element's first occurrence in a list, simply use list.remove
:
(要删除列表中元素的首次出现,只需使用list.remove
:)
>>> a = ['a', 'b', 'c', 'd']
>>> a.remove('b')
>>> print a
['a', 'c', 'd']
Mind that it does not remove all occurrences of your element.
(请注意,它不会删除所有出现的元素。)
Use a list comprehension for that. (为此使用列表理解。)
>>> a = [10, 20, 30, 40, 20, 30, 40, 20, 70, 20]
>>> a = [x for x in a if x != 20]
>>> print a
[10, 30, 40, 30, 40, 70]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…