[英]What is the pythonic way to to resume a loop within a loop
I have an python 2.7 application that I am occasionally interrupting. 我有一个偶尔中断的python 2.7应用程序。
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.
为了处理内存限制,我将数据文件划分为由x和y组件标识的网格。 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).
理想情况下,我想更新y_start和x_start并在我离开的地方恢复应用程序(不处理已处理的网格)。
The main action occurs within a nested for-loop: 主要动作发生在嵌套的for循环内:
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. 在上面的代码中,我已经处理了x_start。 y_start should only be used for when x_min = x_start.
y_start仅用于x_min = x_start时。 For all other values of x, it should start at 0.0.
对于x的所有其他值,它应从0.0开始。
Here's my proposed solution. 这是我建议的解决方案。 Is there a better, more pythonic way:
有没有更好,更Python化的方法:
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()
I'm not sure about a more pythonic way, but you could rewrite it like this (in any language really): 我不确定采用哪种更Python化的方式,但是您可以这样重写它(实际上可以使用任何一种语言):
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()
You could try list comprehension: 您可以尝试列表理解:
[[doAction() for y_min,y_max in y_ranges_resume] if x_min == x_start else [doAction() for y_min,y_max in y_ranges] for x_min,xmax in x_ranges]
Or you could use a lambda function to simplify the condition 或者您可以使用lambda函数来简化条件
correct_y_range = lambda x : y_ranges_resume if x == x_start else y_ranges
[[doAction() for y_min,y_max in correct_y_range(x_min)] for x_min,x_max in x_ranges]
Another option with the lambda function (for better readability) lambda函数的另一个选项(以提高可读性)
correct_y_range = lambda x : y_ranges_resume if x == x_start else y_ranges
for x_min,x_max in x_ranges:
for y_min, y_max in correct_y_range(x_min):
doAction()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.