How to continue in nested loops in Python

后端 未结 6 1776
执念已碎
执念已碎 2020-12-09 01:39

How can you continue the parent loop of say two nested loops in Python?

for a in b:
    for c in d:
        for e in f:
            if somecondi         


        
6条回答
  •  萌比男神i
    2020-12-09 01:56

    1. Break from the inner loop (if there's nothing else after it)
    2. Put the outer loop's body in a function and return from the function
    3. Raise an exception and catch it at the outer level
    4. Set a flag, break from the inner loop and test it at an outer level.
    5. Refactor the code so you no longer have to do this.

    I would go with 5 every time.

提交回复
热议问题