【发布时间】:2012-04-12 12:10:37
【问题描述】:
如果在x 不在列表a 中时调用a.remove(x),我如何忽略“不在列表中”错误消息?
这是我的情况:
>>> a = range(10)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a.remove(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> a.remove(9)
【问题讨论】:
-
为什么 Python 列表中没有 Python set's discard operation?
discard从集合中移除元素(如果存在)。
标签: python list error-handling elements