Python的for循环可以遍历一个列表,但若需要同时将符合条件的元素删除时,则会出错。
出错
1 | s = [1, 2, 3, 4, 1, 1] |
输出:1
[2, 3, 4, 1]
另一种:1
2
3
4
5s2 = s
for idx in range(len(s2)):
if s2[idx] == 1:
del s2[idx]
print(s2)
输出:1
2
3
4Traceback (most recent call last):
File "temp.py", line 11, in <module>
if s2[idx] == 1:
IndexError: list index out of range
出错原因
Python中用for发起任何形式的遍历时,它的遍历顺序都是从最初就确定的,而在遍历中删除了元素会导致当前索引的变化,所以遍历中间取到的索引的那个值已经变了,会漏掉一些元素。另一种错误是删除元素后列表长度缩小了,所以会超出索引范围。
正确删除法
使用
filter()
函数过滤掉符合指定条件的元素。1
2s3 = s
print(list(filter(lambda x: x != 1, s3)))此处
lambda
为Python中函数的简化定义形式。使用列表解析式。
1
2s4 = [i for i in s if i != 1]
print(s4)把原始列表拷贝给一个新列表,遍历新列表,修改原始列表(或相反)。
1
2
3
4
5s6 = s
for i in s6[:]:
if i == 1:
s6.remove(i)
print(s6)但是,下面的赋值操作给新列表是不行的,因为新变量和原变量的物理地址是相同的,可通过
id()
函数查看。1
2
3
4
5s5 = s
for i in s:
if i == 1:
s5.remove(i)
print(s5)可通过深拷贝解决上述问题:
1
2
3
4
5
6
7import copy
s5 = copy.deepcopy(s)
for i in s:
if i == 1:
s5.remove(i)
print(s5)用
while
循环来做判断条件。1
2
3
4s7 = s
while 1 in s7:
s7.remove(1)
print(s7)
上述4种方法的输出皆为:[2, 3, 4]
。
参考
https://segmentfault.com/a/1190000007214571
http://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html