V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Sin
V2EX  ›  Python

除了设flag,有没有什么方法能跳过下一次循环?

  •  
  •   Sin · 2013-02-05 17:51:57 +08:00 · 3756 次点击
    这是一个创建于 4113 天前的主题,其中的信息可能已经有所发展或是发生改变。
    像这样:
    for i in [1,2,3]:
     if i==1:
      if xxx:
       跳过下一次循环,直接进入i=3的情况
    10 条回复    1970-01-01 08:00:00 +08:00
    wog
        1
    wog  
       2013-02-06 01:42:36 +08:00
    for i in [1,2,3]:
     if i==2:
    conitnue
    print(i)
    wog
        2
    wog  
       2013-02-06 01:43:25 +08:00
    囧continue都拼错了,你改一下吧
    BOYPT
        3
    BOYPT  
       2013-02-06 09:52:57 +08:00
    不可能。
    VYSE
        4
    VYSE  
       2013-02-06 10:02:26 +08:00 via Android
    模仿C循环方式不就行了
    yujnln
        5
    yujnln  
       2013-02-06 10:06:58 +08:00
    print [i for i in [1,2,3] if i!=2]
    sivacohan
        6
    sivacohan  
       2013-02-06 21:06:37 +08:00
    goto~
    dotbuddle
        7
    dotbuddle  
       2013-02-06 22:39:49 +08:00
    列表是不是也有index?
    Narcissu5
        8
    Narcissu5  
       2013-02-07 00:22:05 +08:00
    用枚举器循环,continue的同时next一下(好像更麻烦的说-_-!)
    yuelang85
        9
    yuelang85  
       2013-02-07 00:55:55 +08:00   ❤️ 2
    如果是遍历列表的话,可以remove/pop掉下一个元素

    >>> l = [1,2,3,4]
    >>> for i, v in enumerate(l):
    ... if v == 2:
    ... x = l.pop(i+1)
    ... print v
    ...
    1
    2
    4
    yuelang85
        10
    yuelang85  
       2013-02-07 00:56:24 +08:00
    >>> l = [1,2,3,4]
    >>> for i, v in enumerate(l):
    ... ...if v == 2:
    ... ......x = l.pop(i+1)
    ... ...print v
    ...
    1
    2
    4
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1135 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:18 · PVG 07:18 · LAX 16:18 · JFK 19:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.