我試着通過指定列表中我要刪除的索引刪除列表中的項目,但我得到一個錯誤。蟒蛇從列表中刪除項目通過指定索引
我的代碼:
tuu = [1,2,3,4,'nan', 8]
for i in range(len(tuu)):
if tuu[i] == 'nan':
del tuu[i]
,但我得到的錯誤:
7 for i in range(len(tuu)):
----> 8 if tuu[i] == 'nan':
9 del tuu[i]
IndexError: list index out of range
嘗試remove方法。 – Elan
'[我爲我如果我!='南'''' –
當你想在迭代時使用'range(len(list))'''在for循環中刪除項目,你應該刪除[reverse way] (http://stackoverflow.com/questions/35618307/how-to-transform-string-into-dict/35618686#35618686)(查看鏈接) – Ian