不是真的,但你可以使用一个变量continue在第一次继续之后再次告诉它:
continue_again = False
for thing in things:
if continue_again:
continue_again = False
continue
# ...
if some_condition:
# ...
continue_again = True
continue
# ...
Run Code Online (Sandbox Code Playgroud)
使用迭代器:
>>> list_iter = iter([1, 2, 3, 4, 5])
>>> for i in list_iter:
... print "not skipped: ", i
... if i == 3:
... print "skipped: ", next(list_iter, None)
... continue
...
not skipped: 1
not skipped: 2
not skipped: 3
skipped: 4
not skipped: 5
Run Code Online (Sandbox Code Playgroud)
使用next内置的默认值None避免提高StopIteration- 感谢kevpie的建议!
| 归档时间: |
|
| 查看次数: |
2157 次 |
| 最近记录: |