2015-06-05 31 views
0

我有一段代码,其中有几个循环。通过检查条件(使用if语句)我想从除最后一个之外的所有循环中出来。有没有办法做到这一点,而无需修改我的代码,因为我没有多少时间来提交它。打破了for循环的混合物,如果条件

for file in dirs: 
    .... 
    .... 
    #Opens a file in the directory 
    with open(path, 'rU') as csvfile: 

     .... 
     .... 
     .... 
     #Iterates over every row in the File 
     for row in csvreader: 
      ... 
      #If the data is insufficient, the next row will be iterated. 
      if len(Parameters)>15:      
      #If this condition is not satisfied then I need to go to the next iteration of the first 'for' loop 
      #Without Calculating the Average_Slip 
      .... 
      .... 
      Calculation_of_Slip() 
      #Because when all the rows are done iterating, Average_Slip encounters an error as the input values depend on the Slip. 
      #Instead I need to go to the next file in the directory 
    Average_slip(Slip_3MW,Slip_7MW,Slip_9MW,Counter_1, Counter_2, Counter_3, Counter_4) 

我确定我错过了一些非常简单的东西,但任何人都可以帮助我。

回答

2

基于an answer to a previous similar question:您可以将您的代码重构为函数的嵌套循环,并将其与return分开。

这也会增加可读性,因为如果层次太多,嵌套流控制可能会变得混乱。