打破for循环和if条件的混合

Nikki_Champ

我有一段代码,其中包含多个循环。通过检查条件(使用if语句),我想走出除最后一个循环以外的所有循环。有没有一种方法可以在不修改我的大部分代码的情况下完成,因为我有很少的时间来提交它。

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)

我确信我缺少一些非常简单的东西,但是任何人都可以帮助我。

基于对先前相似问题的回答:您可以将嵌套循环重构为一个函数并用破坏它的代码return

这也将增加可读性,因为如果级别太多,嵌套的流控制可能会变得一团糟。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章