python遍历删除集合元素

    技术2022-07-10  128

    1、拷贝删除

    queue = ['a, 'b', 'c', 'a', 'b'] print(queue) for item in queue[:]: if item == 'a' or item == 'b': queue.remove(item) print(queue)

    2、倒叙删除

    queue = ['a, 'b', 'c', 'a', 'b'] print(queue) for i in range(len(collect) - 1, -1, -1): if collect[i] == 'a' or collect[i] == 'b': del collect[i] print(collect)
    Processed: 0.013, SQL: 9