如何重新启动在python中循环? [英] how to restart "for" loop in python ?

查看:259
本文介绍了如何重新启动在python中循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中做到这一点?

How can I do this in python:

x = [1,2,3,4,5,6]
for i in x:
    if i == 4:
       -restart the loop from beginning-
    else:
        print i

所以在这里它将打印到4,然后重复循环

So here it will print till 4 then repeat the loop

推荐答案

这是怎么回事:

x = [1,2,3,4,5,6]
restart = True
while restart:
    for i in x:
        # add any exit condition!
        # if foo == bar:
        #   restart = False
        #   break
        if i == 4:
           break
        else:
            print i

这篇关于如何重新启动在python中循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆