一个关于 for 和 while 语句的问题 - 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

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

  •  
  •   fengyi0524 Nov 5, 2019 2975 views
    This topic created in 2380 days ago, the information mentioned may be changed or developed.
    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 replies    2019-11-10 19:30:17 +08:00
    ZeldaPeach
        1
    ZeldaPeach  
       Nov 5, 2019   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  
       Nov 5, 2019
    官方文档
    docs.python.org/3/reference/compound_stmts.html#the-for-statement
    这一小节最后的 note 把这个使用 internal counter 的迭代机制讲了
    XIVN1987
        3
    XIVN1987  
       Nov 6, 2019
    不能在遍历 sandwich_orders 的同时对 sandwich_orders 进行增减 item 吧
    eggs
        4
    eggs  
       Nov 10, 2019 via iPhone
    不行
    About     Help     Advertise     Blog     API     FAQ     Solana     3037 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 45ms UTC 15:14 PVG 23:14 LAX 08:14 JFK 11:14
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86