data = [11,1,1,1,33,1,1,1,133,44,55]
for each in data:
if each == 1:
data.remove(1)
print(data)
输出:
[11, 33, 1, 1, 133, 44, 55]
答案 0 :(得分:2)
您应该避免在迭代时修改可迭代对象,因为这样会使索引混乱。
使用列表推导轻松过滤数据:
[i for i in data if i!=1.]
# [11, 33, 133, 44, 55]
或者:
new_list = []
for each in data:
if each != 1:
new_list.append(each)