有沒有一種簡單的方法能夠按值刪除列表元素? - Is there a simple way to delete a list element by value?

問題:

a = [1, 2, 3, 4]
b = a.index(6)

del a[b]
print a

The above shows the following error: 上面顯示瞭如下錯誤: ide

Traceback (most recent call last):
  File "D:\zjm_code\a.py", line 6, in <module>
    b = a.index(6)
ValueError: list.index(x): x not in list

So I have to do this: 因此我必須這樣作: this

a = [1, 2, 3, 4]

try:
    b = a.index(6)
    del a[b]
except:
    pass

print a

But is there not a simpler way to do this? 可是,沒有簡單的方法能夠作到這一點嗎? url


解決方案:

參考一: https://stackoom.com/question/Bifc/有沒有一種簡單的方法能夠按值刪除列表元素
參考二: https://oldbug.net/q/Bifc/Is-there-a-simple-way-to-delete-a-list-element-by-value
相關文章
相關標籤/搜索