我试着通过指定列表中我要删除的索引删除列表中的项目,但我得到一个错误。蟒蛇从列表中删除项目通过指定索引
我的代码:
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