python遍歷刪除集合元素

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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章