简体   繁体   English

python while if 循环跳过代码行 (j += 1)

[英]python while if loop skips line of code (j += 1)

I would really appreciate any help I can get.我真的很感激我能得到的任何帮助。

I have 16x16 DataFrame 'DTA' (index - city of origin; columns - city of arrival) and number of flights made as values I have selected flights above certain value (20 in this example) and trying to make new DataFrame (column names 'Departure', 'Arrival' and 'Number of Flights') for those TOP X flights我有 16x16 DataFrame 'DTA'(索引 - 始发城市;列 - 到达城市)和作为值的航班数量我选择了高于某个值的航班(在本例中为 20)并尝试制作新的 DataFrame(列名' TOP X 航班的出发”、“到达”和“航班数量”)

I doing it through a while if loop (I have tried for loop as well, but keep failing) This looks close, but我通过一段时间 if 循环来完成它(我也尝试过 for 循环,但一直失败)这看起来很接近,但是

here is the code first这是代码

n = 0
i = 0
j = 0
while n < len(DTA):
    while i < len(DTA):
        nfn = nf[arrival[n]]
        if i <= k:
            print(i)
            print(j)
            print(n)
            print(k)
            print(' ')
            df['Arrival'][i] = arrival[n]
            df['Number of Flights'][i] = nfn[j]
            i += 1
            j += 1
        else:
            n += 1
            j = 0

            k += len(nfn)

arrival - list of names (in order) of cities that the planes arrived at.到达 - 飞机到达城市的名称列表(按顺序)。 nfn - shows cities of origin with flights exceeding X, like so nfn - 显示航班超过 X 的始发城市,如下所示

origin
Chicago      29.0
Miami        29.0
Naples       44.0
Phoenix      25.0
Teterboro    47.0

As you probably noticed I've put a lot of prints to keep track of my variables here is the result:正如您可能注意到的那样,我已经打印了很多照片来跟踪我的变量,结果如下:

0
0
0
0
 
1
0
1
5
 
2
1
1
5
 
3
2
1
5
 
4
3
1
5

In my understanding it should increase both i and j by 1 each iteration before increasing n, but it skips over j and I have no idea why.在我的理解中,它应该在每次迭代之前将 i 和 j 都增加 1,然后再增加 n,但它会跳过 j,我不知道为什么。

len(DTA) = 16
len(nfn[0]) = 5
len(nfn[1]) = 1

also loop doesn't stop, but 1 problem at the time.循环也不会停止,但当时有 1 个问题。 Would appreciate all and every input将不胜感激所有和每一个输入

If you would put prints in the else case you would probably find the mistake.如果您将打印件放在其他情况下,您可能会发现错误。 Your condition tells "i<=k".你的情况告诉“i<=k”。 k initially is zero. k 最初为零。 So you go into the if case where i is increased by one.因此,您将 go 置于 i 加一的 if 情况下。 Then in the next iteration you go into else and j is reset to zero.然后在下一次迭代中,您将 go 转换为 else 并将 j 重置为零。

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

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