简体   繁体   English

控制for循环(python)

[英]taking control of a for loop (python)

I have program which contains a for loop inside another for loop, which produces a line and i have an if statement that does a check for a key in the line. 我有一个程序,其中包含一个for循环,另一个for循环中包含一个for循环,该循环产生一行,并且我有一个if语句,该语句检查该行中的键。

here is an example 这是一个例子

list1=[var1,var2,var3]

list2 = [file1,file2,file3]


for v in list1:
     //do stuff that returns a string, string.splitlines()
    for f in list2:
        for line in string  
           if key in line and f in line:
               print "line"
              break
           else:
              continue

I get the result I'm looking for, but I want to control the loop, by that I mean in the first iteration we have var1 and the it will loop through file1,file2 and file3 and then next iteration var2 and it will loop through file1,file2,file3 so on and so forth 我得到了我想要的结果,但是我想控制循环,这意味着我在第一个迭代中拥有var1,它将遍历file1,file2和file3,然后在下一个迭代var2中它将遍历file1,file2,file3依此类推

How will I change it so that when it finds the line, it won't go further with var1 instead takes var2 and then proceed ? 我将如何更改它,以便在找到行时不会再使用var1而是使用var2然后继续进行操作? I tried following 我尝试了以下

i = iter(list1)
j = iter(list2)

and the following after print line 和下面的打印行之后

if key in line and f in line:
     print "line"
     i.next()
     j.next()
     break

But this doesn't seem to do anything, any tips on how to achieve this ? 但这似乎无能为力,有关如何实现这一目标的任何提示?

I recommend putting the inner two loops into an appropriately-named function. 我建议将内部两个循环放入一个适当命名的函数中。 When you find the key, return from the function. 找到键后,从函数中返回。

list1=[var1,var2,var3]

list2 = [file1,file2,file3]

for v in list1:
     //do stuff that returns a string, string.splitlines()
    find_line(string)

and then: 然后:

def find_line(string):
    for f in list2:
        for line in string  
           if key in line and f in line:
               print "line"
               return
           else:
              continue

You can use a variable to control the breaking of the nested loops: 您可以使用变量来控制嵌套循环的中断:

for v in list1:
     //do stuff that returns a string, string.splitlines()
    done = False
    for f in list2:
        if done = True:
            break
        for line in string  
           if key in line and f in line:
              print "line"
              done = True
              break
           else:
              continue

You can set up a flag to denote when you've found it. 您可以设置一个标志来表示何时找到它。

for v in list1:
 //do stuff that returns a string, string.splitlines()
    flag = False
    for f in list2:
        for line in string:
           if key in line and f in line:
               print "line"
               flag = True
               break
        if flag:
            break

The dummiest solution would be a boolean 最傻的解决方案是布尔值

as

for v in list1:
    # do your code here
    foundCurrentVar = False
    for f in list2:
         if foundCurrentVar: break
         for line in string  
             if key in line and f in line:
                print "line"
                foundCurrentVar = True
                break
             else:
                continue

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

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