假設一段python程式有多重迴圈,我們都知道在乙個迴圈當中,用break是退出當前的迴圈,然後繼續下一次迴圈,但是如何才能跳出多重迴圈呢,實際就是結束所有的迴圈。
思路1::可以定義乙個異常類,在需要退出迴圈的位置,跑出這個異常,就可以了,因為程式在遇到異常的時候,如果沒有捕獲異常的話,就是直接退出程式的執行,利用這個原理,認為的觸發乙個異常,然後把這個異常捕獲到,就可以實現退出多重迴圈,而且不結束程式,繼續往下執行
#encoding=utf-8
class getoutofloop(exception):
pass
try:
for i in range(5):
for j in range(5):
for k in range(5):
if i == j ==k ==3:
raise getoutofloop()#丟擲乙個異常,就會跳出所有迴圈
else:
print i,'---',j,'---',k
except getoutofloop:
pass
print "done"
結果:可以看到在迴圈到特定條件時,直接丟擲異常,然後進行捕獲,使程式跳出迴圈後並不結束,而是接著捕獲異常的位置後面繼續執行。
d:\>python test.py
0 --- 0 --- 0
0 --- 0 --- 1
0 --- 0 --- 2
0 --- 0 --- 3
0 --- 0 --- 4
0 --- 1 --- 0
0 --- 1 --- 1
0 --- 1 --- 2
0 --- 1 --- 3
0 --- 1 --- 4
0 --- 2 --- 0
0 --- 2 --- 1
0 --- 2 --- 2
0 --- 2 --- 3
0 --- 2 --- 4
0 --- 3 --- 0
0 --- 3 --- 1
0 --- 3 --- 2
0 --- 3 --- 3
0 --- 3 --- 4
0 --- 4 --- 0
0 --- 4 --- 1
0 --- 4 --- 2
0 --- 4 --- 3
0 --- 4 --- 4
1 --- 0 --- 0
1 --- 0 --- 1
1 --- 0 --- 2
1 --- 0 --- 3
1 --- 0 --- 4
1 --- 1 --- 0
1 --- 1 --- 1
1 --- 1 --- 2
1 --- 1 --- 3
1 --- 1 --- 4
1 --- 2 --- 0
1 --- 2 --- 1
1 --- 2 --- 2
1 --- 2 --- 3
1 --- 2 --- 4
1 --- 3 --- 0
1 --- 3 --- 1
1 --- 3 --- 2
1 --- 3 --- 3
1 --- 3 --- 4
1 --- 4 --- 0
1 --- 4 --- 1
1 --- 4 --- 2
1 --- 4 --- 3
1 --- 4 --- 4
2 --- 0 --- 0
2 --- 0 --- 1
2 --- 0 --- 2
2 --- 0 --- 3
2 --- 0 --- 4
2 --- 1 --- 0
2 --- 1 --- 1
2 --- 1 --- 2
2 --- 1 --- 3
2 --- 1 --- 4
2 --- 2 --- 0
2 --- 2 --- 1
2 --- 2 --- 2
2 --- 2 --- 3
2 --- 2 --- 4
2 --- 3 --- 0
2 --- 3 --- 1
2 --- 3 --- 2
2 --- 3 --- 3
2 --- 3 --- 4
2 --- 4 --- 0
2 --- 4 --- 1
2 --- 4 --- 2
2 --- 4 --- 3
2 --- 4 --- 4
3 --- 0 --- 0
3 --- 0 --- 1
3 --- 0 --- 2
3 --- 0 --- 3
3 --- 0 --- 4
3 --- 1 --- 0
3 --- 1 --- 1
3 --- 1 --- 2
3 --- 1 --- 3
3 --- 1 --- 4
3 --- 2 --- 0
3 --- 2 --- 1
3 --- 2 --- 2
3 --- 2 --- 3
3 --- 2 --- 4
3 --- 3 --- 0
3 --- 3 --- 1
3 --- 3 --- 2
done
思路2:在多重迴圈中用return,直接進行返回,程式遇到return就會認為程式執行結束,進而退出多個迴圈,這種情況,執行return後,程式不會繼續往下執行,而是直接結束程式
#encoding=utf-8
#函式中,用return會退出多重迴圈
def test():
for i in range(5):
for j in range(5):
for k in range(5):
if i ==j==k==3:
return
else:
print i,'---',j,'---',k
test()
結果:d:\>python test.py
0 --- 0 --- 0
0 --- 0 --- 1
0 --- 0 --- 2
0 --- 0 --- 3
0 --- 0 --- 4
0 --- 1 --- 0
0 --- 1 --- 1
0 --- 1 --- 2
0 --- 1 --- 3
0 --- 1 --- 4
0 --- 2 --- 0
0 --- 2 --- 1
0 --- 2 --- 2
0 --- 2 --- 3
0 --- 2 --- 4
0 --- 3 --- 0
0 --- 3 --- 1
0 --- 3 --- 2
0 --- 3 --- 3
0 --- 3 --- 4
0 --- 4 --- 0
0 --- 4 --- 1
0 --- 4 --- 2
0 --- 4 --- 3
0 --- 4 --- 4
1 --- 0 --- 0
1 --- 0 --- 1
1 --- 0 --- 2
1 --- 0 --- 3
1 --- 0 --- 4
1 --- 1 --- 0
1 --- 1 --- 1
1 --- 1 --- 2
1 --- 1 --- 3
1 --- 1 --- 4
1 --- 2 --- 0
1 --- 2 --- 1
1 --- 2 --- 2
1 --- 2 --- 3
1 --- 2 --- 4
1 --- 3 --- 0
1 --- 3 --- 1
1 --- 3 --- 2
1 --- 3 --- 3
1 --- 3 --- 4
1 --- 4 --- 0
1 --- 4 --- 1
1 --- 4 --- 2
1 --- 4 --- 3
1 --- 4 --- 4
2 --- 0 --- 0
2 --- 0 --- 1
2 --- 0 --- 2
2 --- 0 --- 3
2 --- 0 --- 4
2 --- 1 --- 0
2 --- 1 --- 1
2 --- 1 --- 2
2 --- 1 --- 3
2 --- 1 --- 4
2 --- 2 --- 0
2 --- 2 --- 1
2 --- 2 --- 2
2 --- 2 --- 3
2 --- 2 --- 4
2 --- 3 --- 0
2 --- 3 --- 1
2 --- 3 --- 2
2 --- 3 --- 3
2 --- 3 --- 4
2 --- 4 --- 0
2 --- 4 --- 1
2 --- 4 --- 2
2 --- 4 --- 3
2 --- 4 --- 4
3 --- 0 --- 0
3 --- 0 --- 1
3 --- 0 --- 2
3 --- 0 --- 3
3 --- 0 --- 4
3 --- 1 --- 0
3 --- 1 --- 1
3 --- 1 --- 2
3 --- 1 --- 3
3 --- 1 --- 4
3 --- 2 --- 0
3 --- 2 --- 1
3 --- 2 --- 2
3 --- 2 --- 3
3 --- 2 --- 4
3 --- 3 --- 0
3 --- 3 --- 1
3 --- 3 --- 2
python中列表刪除和多重迴圈退出
在學習python的時候,會有一些梗非常不適應,在此列舉列表刪除和多重迴圈退出的例子 列表刪除裡面的坑 比如我們有乙個列表裡面有很多相同的值,假如 nums 1,6,6,3,6,2,10,2,100 我想去掉6,可以這樣寫 nums 1 6,6 3,6 2,10 2,100 for n in num...
java中怎麼退出多重迴圈
特此記錄一下,因為今天做的雙重for迴圈沒有退出,直接導致棧溢位,程式掛掉,特此記錄,長個記性 因為業務需求,一般是如果裡層迴圈不滿足,則跳出裡層的,但是外層的繼續迴圈,所以採用continue,大家寫for迴圈的時候一定要記得關閉,不然就會造成死迴圈,程式就掛了 lablea for string...
python列表刪除和多重迴圈退出原理詳解
在學習python的時www.cppcns.com候,會有一些梗非常不適應,在此列舉列表刪除和多重迴圈退出的例子 列表刪除裡面的坑 比如我們有乙個列表裡面有很多相同的值,假如 nums 1,6,6,3,6,2,10,2,100 我想去掉6,可以這樣寫 nums 1,6,6,3,6,2,10,2,10...