0

I am using the below code.

for x in range(100):
        print('inside 1st loop')
        for y in range(25,60):
            print('inside second loop')
            if y >= x:
            print ('y is now greater than x')

Is it possible that once the if condition is satisfied in the inner for loop, the outer loop breaks itself, after 5 more inner loops are run.

Actual code is as below :

for i in range(len(a1)):
            title_derived = []
            print(i)
            for j in range(len(b1)):
                #print(b1.iloc[i][10], a1.iloc[j][3])
                if b1.iloc[j][10] == a1.iloc[i][3]:
                    print('1st if ' + str(j))
                    print (b1.iloc[j][1], a1.iloc[i][11], b1.iloc[j][5])
                    if (((pd.to_datetime(b1.iloc[j][1]) <= pd.to_datetime(a1.iloc[i][11]) <= pd.to_datetime(b1.iloc[j][5]))) or ((pd.to_datetime(b1.iloc[j][1]) <= pd.to_datetime(a1.iloc[i][8]) <= pd.to_datetime(b1.iloc[j][5])))) :
                        print('2nd if' + str(j))
                        title_derived.append(b1.iloc[j][15])
                        print('inserted ' + b1.iloc[j][15] + ' in ' + str(i) + ' th record ')
            a1.iat[i,65] = title_derived 

Now here, I have two dataframes, each record in first (10000 approx records) looks up every record in other dataframe (40000 records). Sometimes there can be atmost 4-5 consecutive entries that match the condition.

So, once the condition is satsfied in second loop, I would like to finish five more iterations and break it.

Sarang Manjrekar
  • 1,839
  • 5
  • 31
  • 61
  • Put the code in a function and `return` from the inner loop. – BoarGules May 18 '18 at 07:53
  • 1
    Possible duplicate of: [Breaking out nested loops](https://stackoverflow.com/questions/653509/breaking-out-of-nested-loops) – Alberto May 18 '18 at 07:58
  • The best solution will depend on your actual code, so the more realistic the sample code you give us, the better. Right now it's trivial. – Alex Hall May 18 '18 at 08:07
  • @AlexHall actual code is very specific to my case. Let me try to add it, if that helps. – Sarang Manjrekar May 18 '18 at 08:11
  • You cannot break an outer loop from an inner loop as the `break`-command will always reflect of the innermost loop it is nesting in. For `for`-loops this behavior is very much intentional, because for loops should (only) be used when you know the scope of iterations and it is fixed. Otherwise the use of while loops is recommended. – mzoll May 18 '18 at 08:14
  • Do you always want to run 5 more iterations, or is it more suitable to continue running until there is an entry that does not match? It sounds like the matching entries are consecutive. – Mattias Backman May 18 '18 at 08:45
  • 5 more iterations will do, since 5 is the max entries ever that will match. – Sarang Manjrekar May 18 '18 at 08:59

2 Answers2

0

Assuming Condition to break the loop, if outer variable greater than inner variable ->break the loop

def inner_loop_function(outer_loop_variable):
  for y in range(25,60):
    if y<=first_loop_var:
      return True
  return False

Outer Loop

for x in range(0, 100):
  print("1st Loop", x)
  var = inner_loop_function(x)
  if var == True:
    # Breaking outer Loop
    break
Surya Tej
  • 1,342
  • 2
  • 15
  • 25
0

Try adding a status variable to indicate when the inner loop is done.

Note that your example will end immediately since 25>0.

done = False
for x in range(100):
    if done is True:
        print('Inner loop is done')
        break

    print ('inside 1st loop')
    print x
    for y in range(25,60):
        print ('inside second loop')
        print y
        if y >= x:
            print ('y is now greater than x')
            done = True
            break
Mattias Backman
  • 927
  • 12
  • 25