多重嵌套循环在Python问题,怎么解决

2024-11-23 16:56:57
推荐回答(1个)
回答1:

使用自定义异常可以跳出深层嵌套循环、看我做过的例子:
class FoundException(Exception): pass
try:
for row,record in enumerate(table):
for columu,field in enumerate(record):
for index,item in enumerate(field):
if item == target:
raise FoundException()
except FoundException:
print ("found at ({0},{1},{2})".format(row,column,index))
else:
print ('not found')