Почему моя переменная условия while-цикла мутирует без присваивания ей? - PullRequest
0 голосов
/ 07 мая 2019

У меня есть следующий бесконечный цикл 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.

1 Ответ

1 голос
/ 07 мая 2019

Вы меняете i непосредственно перед этим print:

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