python退出多重循环
假设一段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中断多重循环的几种思路exit_flag
常见方法 II. 跳出多重循环 事实上,Python的标准语法是不支持跳出多重循环的,所以只能利用一些技巧,大概的思路有:写成函数.利用笛卡尔积.利用调试. 写成函数 在Python中,函数运行到re ...
- Python中断多重循环的几种思路
I. 跳出单循环 不管是什么编程语言,都有可能会有跳出循环的需求,比如枚举时,找到一个满足条件的数就终止.跳出单循环是很简单的,比如 for i in range(10): if i > 5: ...
- Javascript中快速退出多重循环的技巧
在双重循环或多重循环中判断条件,条件符合时跳出整个嵌套循环体是常见的程序逻辑.在Javascript中有哪些跳出的方法呢?楼主简单整理了一下. 一. 使用多个break语句跳出 var breaked ...
- python跳出多重循环
# -*- coding=utf-8 -*- """ 如何结束多重循环,在单层循环中,可以用break跳出循环,那两层,三层呢? """ # ...
- Python 跳出多重循环
Python 本身没有“break n” 和“goto” 的语法,这也造成了Python 难以跳出多层(特定层数)循环.下面是几个跳出多层(特定层数)循环的tip. 1.自定义异常 class g ...
- python跳出多重循环的方法
方法1:自定义异常 # -*- coding:utf-8 -*- """ 功能:python跳出循环 """ # 方法1:自定义异常 cla ...
- python中列表删除和多重循环退出
在学习python的时候,会有一些梗非常不适应,在此列举列表删除和多重循环退出的例子: 列表删除里面的坑 比如我们有一个列表里面有很多相同的值,假如:nums=[1,6,6,3,6,2,10,2,10 ...
- Python之 continue继续循环和多重循环
Python之 continue继续循环 在循环过程中,可以用break退出当前循环,还可以用continue跳过后续循环代码,继续下一次循环. 假设我们已经写好了利用for循环计算平均分的代码: L ...
- java 跳出多重循环
public class Main { public static void main(String[] args) { System.out.println("start"); ...
随机推荐
- sprint boot 配置
来源:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-configure-to ...
- dinner vs supper
用dinner还是supper? 据我唯一认识一个美国人……讲,至少在美国他们用dinner,supper也许在英国更常用些. 他在小时候都没听说过supper这个词…… 另外,have dinner ...
- 在CentOS6.8下安装Docker
在CentOS6.8下安装Docker 一.查看系统版本 [root@localhost opt]# uname -a Linux localhost.localdomain -.el6.x86_64 ...
- MapReduce规约
深入了解Combiners编程(相当于Map端的Reduce) 每一个map可能会产生大量的输出,combiner的作用就是在map端对输出先做一次合并,以减少传输到reducer的数据量. comb ...
- Tarjan-LCA算法小记
Tarjan-LCA算法是一种离线算法. 算法描述: DFS遍历每个节点,对于遍历到的当前节点u: ①建立以u为代表元素的集合. ②遍历与u相连的节点v,如果没有被访问过,对于v使用Tarjan-LC ...
- LAMP下安装zabbix流水
一.安装zabbix (1)创建用户和组 [root@dbking zabbix-2.2.1]# groupadd zabbix [root@dbking zabbix-2.2.1]# useradd ...
- Rhino
http://shrinksafe.dojotoolkit.org/ a JavaScript interpreter
- swiper跳转制定页面
haha(){ var that=this; that.$refs.mySwiper.swiper.slideTo(1, 1000, false); } //以上代码是 获取ref值为myswipe ...
- 【pyqt5】QdateTimeEdit(日期时间)
返回当前日期和时间设置 from PyQt5 import QtCore, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): ...
- JS DOM节点
html代码: <body onload ="loaded12()"> <form name="form1" action="htt ...