4

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.

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

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 ? 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 ?

1
  • So you basically want to break out of nested loops? Commented Jul 26, 2012 at 15:42

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

I think this is the cleanest way to accomplish this in python. (surprisingly enough, I know how to do this in Fortran, but not python ...)
Thank you :), I took one of the solutions provided and made that into a function.
@cyberbemon, I suspect I might not have explained clearly enough. I just added a code example to illustrate. The particular thing to note here is that the find_line function (which you, with your superior knowledge of your domain, can undoubtedly find a better name for :) ) contains a number of loops. Because the return statement exits the whole find_line function, it bails out of all of those loops at once. The chief virtue of this approach is that it avoids any intricate uses of condition variables to decide when to end loops.
@lain, That is exactly what I did and your first answer was clear enough. Thanks again for taking the time to explain things. :)
1

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.