在python中,是否可以进行双连续操作并跳转到列表中下一项之后的项?
发布于 2011-04-25 01:01:42
使用迭代器:
>>> 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
使用默认为None
的next
内置可以避免引发StopIteration
--谢谢您的建议!
发布于 2011-04-25 00:39:58
不完全是,但您可以使用一个变量在第一次继续之后再次告诉continue
:
continue_again = False
for thing in things:
if continue_again:
continue_again = False
continue
# ...
if some_condition:
# ...
continue_again = True
continue
# ...
发布于 2017-09-24 20:02:53
你可以递增迭代器吗?
for i in range(len(list)):
i = i + 1
continue
https://stackoverflow.com/questions/5774200
复制相似问题