for ele in eles:
for i in xrange(10):
try:
#do something
break
except:
continue
else:
if some condition:
continue
#do something
考虑上面的代码,我知道break
将打破内部for循环,第一个continue
将继续内部for循环,第二个continue
呢?它将继续哪个for循环?
最新continue
的效果在外for
循环上生效,如以下示例所示:
>>> for x in [1, 2, 3]:
... for y in [4, 5, 6]:
... print('x =', x, 'y =', y)
... else:
... continue
... print('here')
...
x = 1 y = 4
x = 1 y = 5
x = 1 y = 6
x = 2 y = 4
x = 2 y = 5
x = 2 y = 6
x = 3 y = 4
x = 3 y = 5
x = 3 y = 6
请注意,“这里”永远不会被打印。
另外,请注意,内部for循环无法以任何方式继续:else
迭代器用尽时(在我的示例中:当所有y
s in[4, 5, 6]
都已打印时)并且未break
执行任何语句时,将执行该块。由于迭代器已经用尽,因此无法使其产生更多的值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句