ZeldaPeach's recent timeline updates
ZeldaPeach

ZeldaPeach

V2EX member #428369, joined on 2019-07-10 14:34:15 +08:00
业余编程爱好者,非程序员
ZeldaPeach's recent replies
Nov 5, 2019
Replied to a topic by fengyi0524 Python 一个关于 for 和 while 语句的问题
官方文档
docs.python.org/3/reference/compound_stmts.html#the-for-statement
这一小节最后的 note 把这个使用 internal counter 的迭代机制讲了
Nov 5, 2019
Replied to a topic by fengyi0524 Python 一个关于 for 和 while 语句的问题
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 结束
用 CSS Selector 同时选取图片和文本 img+p
{k: data[k] for k in ('a','b')}
我单位电脑装的趋势杀毒网络版,正常卸载要密码,被我用 360 安全卫士强制把软件安装文件夹给粉碎掉了 23333
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2962 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 28ms · UTC 05:27 · PVG 13:27 · LAX 22:27 · JFK 01:27
♥ Do have faith in what you're doing.