Hướng dẫn break outer loop python
We all know that Python is an elegant programming language. But everything has weaknesses. Sometimes Python is not as elegant as it should be. For example, when we need to break out of nested loops as follows: for a in list_a: Is there an easier way to break out of nested loops than throwing an exception? (In Perl, you can give labels to each loop and at least continue an outer loop.)
I.e., is there a nicer way than:
asked Mar 17, 2009 at 9:24
Michael KuhnMichael Kuhn 7,8356 gold badges25 silver badges26 bronze badges 3
The same works for deeper loops:
Bob Stein 15k8 gold badges79 silver badges98 bronze badges answered Mar 17, 2009 at 12:27
Markus JarderotMarkus Jarderot 84.6k20 gold badges135 silver badges137 bronze badges 17 It has at least been suggested, but also rejected. I don't think there is another way, short of repeating the test or re-organizing the code. It is sometimes a bit annoying. In the rejection message, Mr van Rossum mentions using answered Mar 17, 2009 at 9:27
3 If you're able to extract the loop code into a function, a
If it's hard to extract that function you could use an inner function, as @bjd2385 suggests, e.g.
answered Mar 17, 2009 at 12:37
Mr FoozMr Fooz 106k5 gold badges69 silver badges100 bronze badges 2
Use itertools.product!
Here's a link to itertools.product in the python documentation: http://docs.python.org/library/itertools.html#itertools.product You can also loop over an array comprehension with 2 fors in it, and break whenever you want to.
John R Perry 3,6932 gold badges35 silver badges59 bronze badges answered Oct 26, 2011 at 17:47
Fábio SantosFábio Santos 3,7011 gold badge24 silver badges31 bronze badges 5 Sometimes I use a boolean variable. Naive, if you want, but I find it quite flexible and comfortable to read. Testing a variable may avoid testing again complex conditions and may also collect results from several tests in inner loops.
answered May 16, 2011 at 20:50
HalberdierHalberdier 1,13411 silver badges15 bronze badges 1 If you're going to raise an exception, you might raise a StopIteration exception. That will at least make the intent obvious. answered Aug 14, 2012 at 0:31
dbndbn 12.2k3 gold badges56 silver badges84 bronze badges 2 You can also refactor your code to use a generator. But this may not be a solution for all types of nested loops. answered Mar 17, 2009 at 10:29
J SJ S 1,0477 silver badges6 bronze badges In this particular case, you can merge the loops with a modern python (3.0 and probably 2.6, too) by using itertools.product. I for myself took this as a rule of thumb, if you nest too many loops (as in, more than 2), you are usually able to extract one of the loops into a different method or merge the loops into one, as in this case. answered Mar 17, 2009 at 9:58
TethaTetha 4,7801 gold badge16 silver badges17 bronze badges |