python list 循环中remov

2020-01-14 11:20:41 浏览数 (1)

代码语言:javascript复制
>>> 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

0 人点赞