简体   繁体   English

如何在python中跳过2层循环中的操作?

[英]How to skip an operation in 2 layers of loop in python?

I am currently working on an ML project and the code structure looks like this.我目前正在做一个 ML 项目,代码结构如下所示。 Basically, I want to train 3 different models for 3 different data.基本上,我想为 3 个不同的数据训练 3 个不同的模型。 Now I know some models fail for some of the products which are fine, I just want to know how I can skip them and move to the next model/product.现在我知道有些模型在某些很好的产品中失败了,我只想知道如何跳过它们并转移到下一个模型/产品。 It is even better if I can completely avoid the looping.如果我能完全避免循环就更好了。 Thanks for your time.谢谢你的时间。

model = ['a', 'b', 'c']
products = ['x', 'y' 'z']

for i in products:
  for j in model:
     if j == 'a':
       #. training sequence  -------> if this fails move to next elif
       #. save results
     elif j == 'b'
       #. training sequence  -------> if this fails move to next elif
       #. save results
     elif j == 'c'
       #. training sequence
       #. save results


Example for the ZeroDivisionError: ZeroDivisionError 示例:

for i in (2,4):
    for j in ('a','b'):
        try:
            if j=='a':
                print(f'a: i={i} result:{1/(2-i)}')
            if j=='b':
                print(f'b: i={i} result:{1/(4-i)}')
        except ZeroDivisionError:
            continue

'b' model acts on 2 and 'a' on 4. Output: “b”模型作用于 2,“a”作用于 4。输出:

b: i=2 result:0.5
a: i=4 result:-0.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM