2010-11-13 98 views
0

刪除元素以正確的方式谷歌沒有發現...... 我想兩種解決方案:蟒蛇:whtat是從列表

1) How to remove an element in a for..in.. loop for a list? 
2) How to remove all the elements in the list? 

回答

4
  1. 這是幾乎從來沒有你想要做什麼。使用列表理解來過濾掉你不想要的項目。

  2. del L[:]

-1
1) filter() 
2) list = [] 
+2

1)在大多數情況下醜陋的,但不值得一downvote。 2)完全錯誤。它只是創建一個新列表並將其分配給'list'(唉),而不實際刪除列表中的任何元素。 – aaronasterling 2010-11-13 12:46:54

+0

是的,你是對的。 – 2010-11-13 12:51:07

+0

此外,列表解析應該優先於filter() – helpermethod 2010-11-13 13:35:28