简体   繁体   English

奇怪的嵌套循环无法正确中断(Python 3.x)

[英]Odd Nested Loop Doesn't Break Properly (Python 3.x)

The following code should print multiple lines of 以下代码应打印多行

1
2
3

mixed with lines of 混杂着

0

However, what it actually prints is multiple lines of 但是,它实际打印的是多行

1
1
1
1
3

mixed with lines of 混杂着

0

Code: 码:

boxes = []
for y in range(len(hmap)):
    for x in range(len(hmap[y])):
        w = 4
        h = 4

        minh = hmap[y][x]
        maxh = hmap[y][x]

        htemp = h
        while True:
            if y + htemp > len(hmap): break

            passes = False
            wtemp = w
            while True:
                if x + wtemp > len(hmap[y]): break

                for c in range(x, x+wtemp):
                    for r in range(y, y+htemp):
                        minh = min(minh,hmap[c][r])
                        maxh = max(maxh,hmap[c][r])

                        if maxh - minh > v:
                            print('1')
                            break
                    else:
                        print('2')
                        break
                else:
                    print('3')
                    break

                print('0')
                passes = True
                wtemp += 1

            if passes:
                boxes.append([x,y,wtemp-1,htemp])

            htemp += 1

            if not passes: break
  • hmap is a 2D array of float values that is passed to the function this code is in. hmap是2D浮点值数组,该数组传递给此代码所在的函数。

This segment of code is supposed to generate a series of rectangles for other (irrelevant) parts of code to use later on. 该代码段应该为其他(不相关)代码部分生成一系列矩形,以供以后使用。 Rectangles that "pass" (min/max values don't have a difference greater than v ) cause “通过”的矩形(最小值/最大值的差不大于v )导致

0

to be printed. 待打印。 Rectangles that don't "pass" should cause 不“通过”的矩形引起

1
2
3

to be printed as the nested for and while loops break. 当嵌套forwhile循环中断时打印。 Why doesn't it work? 为什么不起作用?

While attempting to run your code I encountered an IndexError: list index out of range error. 尝试运行您的代码时,我遇到了IndexError: list index out of range错误。 It appears that you may have transposed your column and row indices. 看来您可能已转置了列和行索引。 Try changing the [c][r] subscripts to [r][c] : 尝试将[c][r]下标更改为[r][c]

# [...]
            for c in range(x, x+wtemp):
                for r in range(y, y+htemp):
                    minh = min(minh,hmap[r][c])
                    maxh = max(maxh,hmap[r][c])
# [...]

I am not sure if this is the cause of the incorrect breaks/prints, but it certainly could make a difference. 我不确定这是否是错误的中断/打印的原因,但是肯定可以有所作为。

The code could be breaking the wrong loops, I could be wrong . 代码可能会打错循环, 我可能是错的 For the while loop, make a Boolean variable and set it to true. 对于while循环,请创建一个布尔变量并将其设置为true。 Then inside a while loop, use an if statement to make it false when you want to. 然后在while循环中,使用if语句将其设置为false。

top_loop, bottom_loop = True, True
while top_loop:
    # do something
    while bottom_loop:
        # do something
        if condition:
            top_loop = False

I haven't though about the for loops yet. 我还没有关于for循环。 There is answer here on this link with the naming for loops and breaking the for loops. 在此链接上,答案是for循环和打破for循环。 It uses the contextlib library. 它使用contextlib库。

Link 链接

It looks like the indentation on your code blocks is incorrect. 您的代码块上的缩进似乎不正确。 There are else statements aligned with for statements, etc. Python uses indentation to separate blocks of code like this. 还有elsefor语句对齐for语句,等等。Python使用缩进来分隔这样的代码块。 Double-check that things are aligned properly, either in your code, or in what you copied here. 仔细检查代码中或此处复制的内容是否正确对齐。 If the indentation is just incorrect in the question here, feel free to edit it. 如果缩进在此问题中只是不正确,请随时对其进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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