python 循环内部添加多个条件判断会出现越界

1.循环遍历数组是,想添加条件修改时,只删除第一个

# -*- coding: utf-8 -*-

a=[11,22,33,44,55]

for i in a:
    if i == 11 or i ==22:
        a.remove(i)

for i in a:
    print(i)

'''
22
33
44
55
[Finished in 0.1s]
'''

2.应该引入被删除为一个数组

# -*- coding: utf-8 -*-

a=[11,22,33,44,55]

b=[]

for i in a:
    if i == 11 or i ==22:
        b.append(i)
for i in b:
    a.remove(i)
for i in a:
    print(i)

'''
33
44
55
[Finished in 0.1s]
'''

 

posted @ 2017-07-09 16:45  AlamZ  阅读(1350)  评论(0编辑  收藏  举报