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

时间:2017-05-17 03:40:41

标签: python

我正在努力实现这样的目标

    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的情况下移动到j的下一次迭代。我试图重新排序for循环,所以j首先出现,但它会影响我的i和j比较。这是我的代码,用于澄清我想要做的事情:

    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是一个字典,其键与j的索引对齐。 buffer是一个列表,k也是一个列表。

谢谢

2 个答案:

答案 0 :(得分:1)

字面意思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.

答案 1 :(得分:-1)

如果要使用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