简体   繁体   English

如何在python中继续进行内部for循环的下一次迭代

[英]how to continue to next iteration of the inner for loop in python

I am trying to achieve something like this 我正在努力实现这样的目标

    for i in range(1,n):
       for j in range(0,len(b)):
          #compare i and j
          if #condition 1:
              #do something 1
          else:
              #compare i and j+1 till len(b) unless condition 1 is encountered
              #if condition one is encountered, do something 1
              #if j gets to len(b) without condition 1:
                  #do something 2

I am trying to put my question in the simplest way I can. 我试图以最简单的方式提出我的问题。 Please how can I get the else part to achieve what I want? 请我如何获得其他部分来实现我想要的? Primarily to move to the next iteration of j without changing i. 主要是在不更改i的情况下移至j的下一个迭代。 I tried to reorder the for loop so j comes first but it would affect my i and j comparison. 我试图重新排序for循环,因此j排在第一位,但这会影响我的i和j比较。 Here is my code for clarification on what I am trying to do: 这是我的代码,以澄清我要做什么:

    for i in range(1,n):
        for j in range(0,len(buffer)):
            _, p = scipy.stats.ks_2samp(k[i], buffer[j]) 
            if p > alpha:
                if (j) in cluster.keys():
                    cluster[j].append(i)
                    break

            if p < alpha:
                 **#this portion here is where my problem lies. I need it to search through the length of j (len(buffer)) to be sure there is no p>alpha before moving on to the next two lines of code.**
                 cluster[i] = [i]
                 buffer.append(k[i])

N/B: cluster is a dictionary whose keys align with the index of j. N / B:cluster是一个字典,其键与j的索引对齐。 buffer is a list and k is a list too. buffer是一个列表,k也是一个列表。

Thanks 谢谢

Literally continue . 从字面上continue

for i in range(0, 10):
    if i % 2 == 0:
        continue
    print(i, 'is odd.')

# 1 is odd.
# 3 is odd.
# 5 is odd.
# 7 is odd.
# 9 is odd.

If you want to use the if statement to move to the next iteration you can do this. 如果要使用if语句移至下一个迭代,则可以执行此操作。

for i in range(1,n):
       for j in range(0,len(b)):
          #compare i and j
          if #condition 1:
              continue
          else:
              #compare i and j+1 till len(b) unless condition 1 is encountered
              #if condition one is encountered, do something 1
              #if j gets to len(b) without condition 1:
                  #do something 2

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

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