简体   繁体   English

带有循环的 Python 计数器/Prob

[英]Python counter / Prob with the loop

I got 2 list of coordinates, X and Y. If we plot them, we would see a more or less circular trajectory.我得到了 2 个坐标列表,X 和 Y。如果我们绘制它们,我们会看到或多或少的圆形轨迹。 My objective is to count, the number of circle trajectory that is done.我的目标是计算完成的圆轨迹数。

The idea of the algorithm:算法思路:

1..I calculate the average center of the trajectory 1..我计算轨迹的平均中心

Center = (int(sum(X)/len(X)), int(sum(Y)/len(Y)))

Center will be X0 and Y0中心将是 X0 和 Y0

2..K is the counter. 2..K 是计数器。 I look where the point is, and everytime it get back in the south / west area, K += 1. Moreover, until i get out of this area, K doesn't stack up anymore.我看点在哪里,每次它回到南/西区域,K += 1。而且,直到我离开这个区域,K 不再叠加。

        K = 0
        c = 0
        while c < len(X):
            if X[c] >= X0 and Y[c] >= Y0:
                c += 1
                continue
            elif X[c] < X0 and Y[c] >= Y0:
                c += 1
                continue
            elif X[c] >= X0 and Y[c] < Y0:
                c += 1
                continue
            else:
                K += 1
                # On saute les points suivants dans le même quart
                while X[c] < X0 and Y[c] < Y0:
                    c += 1

It works, but not if my final points are in this area, in which case, i get an out of range with the while X[c] < X0 and Y[x] < Y0.它有效,但如果我的最终点位于该区域则无效,在这种情况下,当 X[c] < X0 和 Y[x] < Y0 时我会超出范围。

I tried this code, that i found better, but i can't get it to work:我试过这段代码,我发现它更好,但我无法让它工作:

for c in range(len(X)):
                if X[c] >= X0 and Y[c] >= Y0:
                    continue
                elif X[c] < X0 and Y[c] >= Y0:
                    continue
                elif X[c] >= X0 and Y[c] < Y0:
                    continue
                else:
                    K += 1
                    while X[c] < X0 and Y[c] < Y0:
                        # Here i need a continue but on the for, not on the while...

Any help would be priceless.任何帮助都是无价的。

Thanks !谢谢 !

I am hoping that I understood the problem correctly, What you are looking for is to break out of single loop in a nested loop?我希望我正确理解了问题,您正在寻找的是在嵌套循环中跳出单循环? You can use an exception to return from inner loop您可以使用异常从内部循环返回

class Found(Exception): pass
for c in range(len(X)):
       try:
                if X[c] >= X0 and Y[c] >= Y0:
                    continue
                elif X[c] < X0 and Y[c] >= Y0:
                    continue
                elif X[c] >= X0 and Y[c] < Y0:
                    continue
                else:
                    K += 1
                    while X[c] < X0 and Y[c] < Y0:
                         #do whatever you want here               
                         raise Found  #raise exception when condition occurs
        except Found:
               print(K)

You can find below a solution, not really beautiful, but working.您可以在下面找到一个解决方案,虽然不是很漂亮,但是很有效。 It checks where the last position is.它检查最后一个位置在哪里。 If it's in the 1/4 of the circle currently being checked, it changes the checking area to the opposite 1/4.如果它在当前正在检查的圆的 1/4 内,它将检查区域更改为相反的 1/4。

if (X[len(X)-1] >= X0 and Y[len(X)-1] >= Y0) or (X[len(X)-1] < X0 and Y[len(X)-1] >= Y0) or (X[len(X)-1] >= X0 and Y[len(X)-1] < Y0):
            while c < len(X):
                if X[c] >= X0 and Y[c] >= Y0:
                    c += 1
                    continue
                elif X[c] < X0 and Y[c] >= Y0:
                    c += 1
                    continue
                elif X[c] >= X0 and Y[c] < Y0:
                    c += 1
                    continue
                else:
                    K += 1
                    # skip the following points in the same quarter
                    while X[c] < X0 and Y[c] < Y0:
                        c += 1
        else:
            while c < len(X):
                if X[c] <= X0 and Y[c] <= Y0:
                    c += 1
                    continue
                elif X[c] > X0 and Y[c] <= Y0:
                    c += 1
                    continue
                elif X[c] <= X0 and Y[c] > Y0:
                    c += 1
                    continue
                else:
                    K += 1
                    # skip the following points in the same quarter
                    while X[c] > X0 and Y[c] > Y0:
                        c += 1

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

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