python list 循环中remov

发布时间:2019-07-13 11:39:20编辑:auto阅读(1598)

    >>> 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

关键字