What is the pythonic way to to resume a loop within a loop

Larry Freeman

I have an python 2.7 application that I am occasionally interrupting.

I am processing a very large data file. To deal with memory constraints, I have divided up the data file into grids that are identified by an x and y components. Each grid is processed independently.

It takes a very long time to process so occasionally, I need to stop the processing at a certain point. Ideally, I would like to update the y_start and x_start and resume the application at the place I left off (without processing a grid that had already been processed).

The main action occurs within a nested for-loop:

x_start=0.0
x_step=0.05
x_size=10.0
y_start=0.0
y_step=0.05
y_size=10.0

x_ranges = zip(np.arange(x_start,x_size,x_step), np.arange(x_step+x_start,x_size+x_step,x_step))

y_ranges = zip(np.arange(0.0,y_size,y_step), np.arange(y_step,y_size+y_step,y_step))


for x_min,x_max in x_ranges:
    for y_min,y_max in y_ranges:

        doAction()

In the code above, I have the x_start handled. y_start should only be used for when x_min = x_start. For all other values of x, it should start at 0.0.

Here's my proposed solution. Is there a better, more pythonic way:

y_ranges_resume = zip(np.arange(y_start,y_size,y_step),np.arange(y_start+y_step,y_size+y_step,y_step)

for x_min,x_max in x_ranges:
    if x_min == x_start:     
        for y_min,y_max in y_ranges_resume:
            doAction()
    else:
        for y_min,y_max in y_ranges:
            doAction()
Aaron

I'm not sure about a more pythonic way, but you could rewrite it like this (in any language really):

y_ranges_resume = zip(np.arange(y_start,y_size,y_step),np.arange(y_start+y_step,y_size+y_step,y_step)

for x_min,x_max in x_ranges:
    y_ranges_used = y_ranges
    if x_min == x_start:     
        y_ranges_used = y_ranges_resume

    for y_min,y_max in y_ranges_used:
        doAction()

At least then the inner loop is only written once.

Alternatively you could use the ternary, but I error on the side of easier to read rather than smaller code. But for completeness sake you could write the same thing this way:

y_ranges_resume = zip(np.arange(y_start,y_size,y_step),np.arange(y_start+y_step,y_size+y_step,y_step)

for x_min,x_max in x_ranges:
    for y_min, y_max in y_ranges_resume if x_min == x_start else y_ranges:
        doAction()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pythonic way to make dictionary within a for loop

From Java

What is the pythonic way to detect the last element in a 'for' loop?

From Dev

What is the most pythonic way to create a 'For Loop' that filters through a dictionary?

From Dev

What is the pythonic way of running an asyncio event loop forever?

From Java

Pythonic way to combine FOR loop and IF statement

From Dev

Pythonic way to default loop counters

From Dev

Pythonic way to loop over dictionary

From Dev

Pythonic way to break out of loop

From Dev

What is the most efficient way in Selenium to open and process links within a loop?

From Dev

how to change for loop code to pythonic way

From Dev

Pythonic way to detect final iteration of a loop

From Dev

Pythonic way of handling two statements in a for loop?

From Dev

Pythonic way to replace > with < in the midst of a big for loop

From Dev

Pythonic way of executing a loop on a dictionary of lists

From Dev

Pythonic way to join a string in a loop to create textblock

From Dev

Is there a more pythonic way of writing a loop that modifies a list?

From Dev

Is there a pythonic way for loop and use previous item

From Java

What would be the pythonic way to go to prevent circular loop while writing JSON?

From Dev

While loop within a for loop, is there an easier and faster way?

From Dev

Pause and Resume a for Loop

From Dev

Is there are more pythonic way to write a while loop that only updates a variable?

From Dev

How to stop code execution of for loop from try/except in a Pythonic way?

From Dev

Pythonic way of splitting loop over list in two parts with one iterator

From Dev

Pythonic way to broadcast/loop through values when expanding kwargs

From Dev

Pythonic way to loop through dictionary and perform conditional GET request

From Dev

pythonic way to set the elements of a loop to a constant based on a condition

From Dev

Pythonic way to to loop through object attributes and assign new attribute

From Dev

Is there a Pythonic way of skipping if statements in a for loop to make my code run faster?

From Dev

Way to implement looking for NoneType within a for loop?

Related Related

  1. 1

    Pythonic way to make dictionary within a for loop

  2. 2

    What is the pythonic way to detect the last element in a 'for' loop?

  3. 3

    What is the most pythonic way to create a 'For Loop' that filters through a dictionary?

  4. 4

    What is the pythonic way of running an asyncio event loop forever?

  5. 5

    Pythonic way to combine FOR loop and IF statement

  6. 6

    Pythonic way to default loop counters

  7. 7

    Pythonic way to loop over dictionary

  8. 8

    Pythonic way to break out of loop

  9. 9

    What is the most efficient way in Selenium to open and process links within a loop?

  10. 10

    how to change for loop code to pythonic way

  11. 11

    Pythonic way to detect final iteration of a loop

  12. 12

    Pythonic way of handling two statements in a for loop?

  13. 13

    Pythonic way to replace > with < in the midst of a big for loop

  14. 14

    Pythonic way of executing a loop on a dictionary of lists

  15. 15

    Pythonic way to join a string in a loop to create textblock

  16. 16

    Is there a more pythonic way of writing a loop that modifies a list?

  17. 17

    Is there a pythonic way for loop and use previous item

  18. 18

    What would be the pythonic way to go to prevent circular loop while writing JSON?

  19. 19

    While loop within a for loop, is there an easier and faster way?

  20. 20

    Pause and Resume a for Loop

  21. 21

    Is there are more pythonic way to write a while loop that only updates a variable?

  22. 22

    How to stop code execution of for loop from try/except in a Pythonic way?

  23. 23

    Pythonic way of splitting loop over list in two parts with one iterator

  24. 24

    Pythonic way to broadcast/loop through values when expanding kwargs

  25. 25

    Pythonic way to loop through dictionary and perform conditional GET request

  26. 26

    pythonic way to set the elements of a loop to a constant based on a condition

  27. 27

    Pythonic way to to loop through object attributes and assign new attribute

  28. 28

    Is there a Pythonic way of skipping if statements in a for loop to make my code run faster?

  29. 29

    Way to implement looking for NoneType within a for loop?

HotTag

Archive