Python列表搞砸了

时间:2018-07-06 05:01:29

标签: python list loops

所以我有一些代码:

nlist = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,2,0]
count = 0
for i in nlist:
    if nlist[i] == 0:
        nlist.remove(nlist[i])
        count += 1
        if count == 4:
            count = 0
print(nlist)

当我运行它时,输出为:

[0, 0, 0, 0, 0, 0, 2, 0]

即使输出应该是这样:

[2]

有人可以告诉我我做错了什么吗?

0 个答案:

没有答案