python 遍历列表时删除元素

testObjs = ["1","2","3","1"]
# i=0
# for o in testObjs:
#     testObjs.pop(i)
#     print(o)
#     i+=1
j=0
length = len(testObjs)
for i in range(0,length):
    o = testObjs[i-j]
    if(o=="1"):
        testObjs.pop(i-j)
        j+=1

print(testObjs)

结果

posted @ 2021-12-16 00:20  乘舟凉  阅读(97)  评论(0编辑  收藏  举报