list = [1, 10, 3, 20, 60, 100, 6]
想删除第 3,7 个元素,假如只知道下标为 del_index = [2, 6]
下面这样是不对的,因为pop之后会改变index!!
for i in del_index:
list.pop(i)
print(list)
应该:先把元素存起来,再 remove!
del_elems=[]
for i in del_index:
del_elems.append(list[i])
for elem in del_elems:
list.remove(elem)
print(list)
网友评论