为什么我的 while 循环条件变量在没有分配给它的情况下发生突变?

Why does my while-loop condition variable get mutated without assigning to it?

我有以下永无止境的 while 循环:

i = len(taglist) - 1
while(i >= 0):
    print("i, at the beginning: " + str(i))
    tag = taglist[i]
    label = tag["tagname"]
    merged_polygon = tag["polygon"]
    merged_indices = [i]
    print("i, a little further: " + str(i))
    for j in range(i * num_passes):
        print("i, in the for-loop: " + str(i))
        if taglist[j % i]["tagname"] == label and len(intersect(taglist[j % i]["polygon"], merged_polygon)) > 0:
            merged_polygon = unite(taglist[j % i]["polygon"], merged_polygon)
            merged_indices.append(j)
            print("i, at the end of the for-loop: " + str(i))
    taglist = [t for i, t in enumerate(taglist) if i not in merged_indices]
    print("i, after the for-loop: " + str(i))
    tag["polygon"] = merged_polygon
    tag["bbox"] = bound_box(merged_polygon)
    taglist.append(tag)
    print("i, before update: " + str(i))
    i = min([i - 1, len(taglist) - 2])
    print("i, after update: " + str(i))

这导致以下结果被一遍又一遍地打印出来:

...
i, at the beginning: 1
i, a little further: 1
i, in the for-loop: 1
i, in the for-loop: 1
i, in the for-loop: 1
i, in the for-loop: 1
i, in the for-loop: 1
i, after the for-loop: 2
i, before update: 2
i, after update: 1
...

我的 while 循环条件变量 (i) 在嵌套的 for 循环之后递增,而我没有这样做。为什么是这样?我只想在 while 循环结束时更改 ì

您在 print 之前更改 i

taglist = [t for i, t in enumerate(taglist) if i not in merged_indices]