如何在循环中进行双重'继续'?

时间:2011-04-25 00:37:35

标签: python

是否有可能进行双重继续并跳转到python列表中下一项之后的项目?

3 个答案:

答案 0 :(得分:7)

不是真的,但你可以使用变量在第一次继续之后再次告诉continue

continue_again = False
for thing in things:
    if continue_again:
        continue_again = False
        continue
    # ...
    if some_condition:
        # ...
        continue_again = True
        continue
    # ...

答案 1 :(得分:6)

使用迭代器:

>>> 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

使用默认为next的{​​{1}}内置文件可以避免提升None - 感谢kevpie提供建议!

答案 2 :(得分:0)

你能增加迭代器吗?

for i in range(len(list)):
    i = i + 1
    continue