V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Feb 5, 2013 · 4341 views
    This topic created in 4842 days ago, the information mentioned may be changed or developed.
    像这样:
    for i in [1,2,3]:
     if i==1:
      if xxx:
       跳过下一次循环,直接进入i=3的情况
    10 replies    1970-01-01 08:00:00 +08:00
    wog
        1
    wog  
       Feb 6, 2013
    for i in [1,2,3]:
     if i==2:
    conitnue
    print(i)
    wog
        2
    wog  
       Feb 6, 2013
    囧continue都拼错了,你改一下吧
    BOYPT
        3
    BOYPT  
       Feb 6, 2013
    不可能。
    VYSE
        4
    VYSE  
       Feb 6, 2013 via Android
    模仿C循环方式不就行了
    yujnln
        5
    yujnln  
       Feb 6, 2013
    print [i for i in [1,2,3] if i!=2]
    sivacohan
        6
    sivacohan  
    PRO
       Feb 6, 2013
    goto~
    dotbuddle
        7
    dotbuddle  
       Feb 6, 2013
    列表是不是也有index?
    Narcissu5
        8
    Narcissu5  
       Feb 7, 2013
    用枚举器循环,continue的同时next一下(好像更麻烦的说-_-!)
    yuelang85
        9
    yuelang85  
       Feb 7, 2013   ❤️ 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  
       Feb 7, 2013
    >>> l = [1,2,3,4]
    >>> for i, v in enumerate(l):
    ... ...if v == 2:
    ... ......x = l.pop(i+1)
    ... ...print v
    ...
    1
    2
    4
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2974 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 12:12 · PVG 20:12 · LAX 05:12 · JFK 08:12
    ♥ Do have faith in what you're doing.