简体   繁体   English

Python,循环循环的下一次迭代

[英]Python, next iteration of the loop over a loop

I need to get the next item of the first loop given certain condition, but the condition is in the inner loop. 给定特定条件时,我需要得到第一个循环的下一个项,但条件是在内循环中。 Is there a shorter way to do it than this? 有没有比这更短的方法呢? (test code) (测试代码)

    ok = 0
    for x in range(0,10):
        if ok == 1:
            ok = 0
            continue
        for y in range(0,20): 
            if y == 5:
                ok = 1
                continue

What about in this situation? 在这种情况下怎么样?

for attribute in object1.__dict__:
    object2 = self.getobject()
    if object2 is not None:
        for attribute2 in object2: 
            if attribute1 == attribute2:
                # Do something
                #Need the next item of the outer loop

The second example shows better my current situation. 第二个例子显示了我目前的情况。 I dont want to post the original code because it's in spanish. 我不想发布原始代码,因为它是西班牙语。 object1 and object2 are 2 very different objects, one is of object-relational mapping nature and the other is a webcontrol. object1和object2是两个非常不同的对象,一个是对象关系映射的性质,另一个是webcontrol。 But 2 of their attributes have the same values in certain situations, and I need to jump to the next item of the outer loop. 但是它们中的2个属性在某些情况下具有相同的值,我需要跳转到外循环的下一个项目。

Replace the continue in the inner loop with a break . break替换内循环中的continue What you want to do is to actually break out of the inner loop, so a continue there does the opposite of what you want. 你想要做的是实际上打破内循环,所以continue那样做你想要的相反。

ok = 0
for x in range(0,10):
    print "x=",x
    if ok == 1:
        ok = 0
        continue
    for y in range(0,20): 
        print "y=",y
        if y == 5:
            ok = 1
            break

You can always transform into a while loop: 您始终可以转换为while循环:

flag = False
for x in range(0, 10):
    if x == 4: 
        flag = True
        continue

becomes

x = 0
while (x != 4) and x < 10:
    x += 1
flag = x < 10

Not necessary simpler, but nicer imho. 没有必要更简单,但更好的imho。

Your example code is equivalent to (that doesn't seem what you want): 您的示例代码等同于(看起来不像您想要的):

for x in range(0, 10, 2):
    for y in range(20): 
        if y == 5:
           continue

To skip to the next item without using continue in the outer loop: 要在外部循环中不使用continue而跳到下一个项目:

it = iter(range(10))
for x in it:
    for y in range(20):
        if y == 5:
           nextx = next(it)
           continue

So you're not after something like this? 所以你不是在追求这样的事情吗? I'm assuming that you're looping through the keys to a dictionary, rather than the values, going by your example. 我假设你正在通过键来循环到字典,而不是你的例子中的值。

for i in range(len(object1.__dict__)):
  attribute1 = objects1.__dict__.keys()[i]
  object2 = self.getobject() # Huh?
  if object2 is not None:
    for j in range(len(object2.__dict__)):
      if attribute1 == object2.__dict__.keys()[j]:
        try:
          nextattribute1 = object1.__dict__.keys()[i+1]
        except IndexError:
          print "Ran out of attributes in object1"
          raise

It's not completely clear to me what you want, but if you're testing something for each item produced in the inner loop, any might be what you want ( docs ) 我不清楚你想要什么,但是如果你正在为内循环中产生的每个项目测试一些东西,那么any你想要的东西都可以( docs

>>> def f(n):  return n % 2
... 
>>> for x in range(10):
...     print 'x=', x
...     if f(x):
...         if any([y == 8 for y in range(x+2,10)]):
...             print 'yes'
... 
x= 0
x= 1
yes
x= 2
x= 3
yes
x= 4
x= 5
yes
x= 6
x= 7
x= 8
x= 9

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

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