python list 循环中remov

2023-01-31 01:01:30 python list 循环

>>> a = [0,1,2,3,0,0,3]
>>>
for item in a :
    print item
    a.remove(item)
    print a
输出:
0
[1, 2, 3, 0, 0, 3]
2
[1, 3, 0, 0, 3]
0
[1, 3, 0, 3]
3
[1, 0, 3]

解决方式:

# -*- coding: cp936 -*-
list1=[1,2,3,4,5]
list2=list1[:]  #复制一个才能有想像中的效果
for i in list1:
    print i
    list2.remove(i)

list1=list2

相关文章