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
fengyi0524
V2EX  ›  Python

一个关于 for 和 while 语句的问题

  •  
  •   fengyi0524 · 2019-11-05 21:56:26 +08:00 · 2127 次点击
    这是一个创建于 1604 天前的主题,其中的信息可能已经有所发展或是发生改变。
    sandwich_orders = ["pastrami","vegetable","chicken","pastrami","pastrami"]
    finished_sandwiches = []
    #while sandwich_orders:
    for sandwich_order in sandwich_orders:
    print("I made your " + sandwich_order + " sandwich")
    sandwich_orders.remove(sandwich_order)

    输出是:

    I made your pastrami sandwich
    I made your chicken sandwich
    I made your pastrami sandwich

    如果不注释掉 while 语句,就可以全部输出:

    I made your pastrami sandwich
    I made your chicken sandwich
    I made your pastrami sandwich
    I made your vegetable sandwich
    I made your pastrami sandwich

    请问为什么只用 for 语句会出现输出不完整的情况?添加了 while 语句后为什么可以避免出现问题?
    4 条回复    2019-11-10 19:30:17 +08:00
    ZeldaPeach
        1
    ZeldaPeach  
       2019-11-05 23:33:01 +08:00   ❤️ 4
    list 的 for ... in 迭代,是内部生成一个计数器,从 0 开始,每次加 1,将这个数字作为索引取该 list 的元素,直到产生 StopIteration 终止
    内部计数器从 0 开始
    list 为 ["pastrami","vegetable","chicken","pastrami","pastrami"]
    给出 list[0] 用作迭代,此时为 "pastrami"
    删掉"pastrami",list 变为 ["vegetable","chicken","pastrami","pastrami"]
    内部计数器加 1 变为 1
    给出 list[1] 用作迭代,此时为 "chicken",那个 "vegetable" 的索引已经变为 0 了,所以被跳过了
    删掉 "chicken",list 变为 ["vegetable","pastrami","pastrami"]
    内部计数器加 1 变为 2
    给出 list[2] 用作迭代,此时为 "pastrami"
    删掉 "pastrami",list 变为 ["vegetable","pastrami"]
    内部计数器加 1 变为 3
    给出 list[3] 用作迭代,超过现在 list 的长度,触发 StopIteration,迭代终止

    此时如果 while list,因为非空,所以再将 ["vegetable","pastrami"] 进行 for 迭代
    list[0] -> "vegetable",删掉此元素,list 变为 ["pastrami"]
    list[1] -> StopIteration,迭代终止

    再 while list,还有一个元素,所以再将 ["pastrami"] 进行 for 迭代
    list[0] -> "pastrami",删掉此元素,list 变为 []
    list[1] -> StopIteration,迭代终止

    再 while list,空列表,不再继续,while 结束
    ZeldaPeach
        2
    ZeldaPeach  
       2019-11-05 23:39:49 +08:00
    官方文档
    docs.python.org/3/reference/compound_stmts.html#the-for-statement
    这一小节最后的 note 把这个使用 internal counter 的迭代机制讲了
    XIVN1987
        3
    XIVN1987  
       2019-11-06 09:13:14 +08:00
    不能在遍历 sandwich_orders 的同时对 sandwich_orders 进行增减 item 吧
    eggs
        4
    eggs  
       2019-11-10 19:30:17 +08:00 via iPhone
    不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3482 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:00 · PVG 19:00 · LAX 04:00 · JFK 07:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.