简体   繁体   中英

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

I have the following neverending while-loop:

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))

This results in the following results being printed over and over again:

...
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
...

My while-loop condition variable ( i ) is being incremented after the nested for-loop without my doing so. Why is this? I exclusively want to alter ì at the end of the while-loop.

您在print之前立即更改了i

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM