Python 如何删除列表索引位置?

Python 如何删除列表索引位置?,python,list,Python,List,我想删除大于3的numList元素, 但是给了我一个错误。有什么想法吗 IndexError: list assignment index out of range 我可以在列表理解中使用此选项: from datetime import datetime, date,timedelta numList = [[2],[3],[4]] for lst in numList: for i in lst: if i > 3: print(i) del

我想删除大于3的numList元素, 但是给了我一个错误。有什么想法吗

IndexError: list assignment index out of range 

我可以在列表理解中使用此选项:

from datetime import datetime, date,timedelta
numList = [[2],[3],[4]]
for lst in numList:
for i in lst:
    if i > 3:
        print(i)
        del numList[i] # del numList[2] I dont want to use this 
                       #because numList elements are changing
        print("updated numList:",numList)  

将其放入变量中,然后对其进行迭代。(将第一个y括起来以获得[[2],[3]])

我可以在列表理解中使用此选项:

from datetime import datetime, date,timedelta
numList = [[2],[3],[4]]
for lst in numList:
for i in lst:
    if i > 3:
        print(i)
        del numList[i] # del numList[2] I dont want to use this 
                       #because numList elements are changing
        print("updated numList:",numList)  
将其放入变量中,然后对其进行迭代。(将第一个y括到[y]以获得[[2],[3]])

这将有助于您:

[2,3]
输出:

from datetime import datetime, date,timedelta
import copy 
numList = [[2],[3],[4]]
numListcopy = copy.deepcopy(numList) #Take a copy of the original list
for lst in numListcopy: #Iterate over the copied list
    for i in lst:
        if i > 3:
            print(i)
            del numList[numList.index(lst)]
print("updated numList:",numList) 
这将有助于你:

[2,3]
输出:

from datetime import datetime, date,timedelta
import copy 
numList = [[2],[3],[4]]
numListcopy = copy.deepcopy(numList) #Take a copy of the original list
for lst in numListcopy: #Iterate over the copied list
    for i in lst:
        if i > 3:
            print(i)
            del numList[numList.index(lst)]
print("updated numList:",numList)