ZeldaPeach 最近的时间轴更新
ZeldaPeach

ZeldaPeach

V2EX 第 428369 号会员,加入于 2019-07-10 14:34:15 +08:00
业余编程爱好者,非程序员
ZeldaPeach 最近回复了
2019-11-05 23:39:49 +08:00
回复了 fengyi0524 创建的主题 Python 一个关于 for 和 while 语句的问题
官方文档
docs.python.org/3/reference/compound_stmts.html#the-for-statement
这一小节最后的 note 把这个使用 internal counter 的迭代机制讲了
2019-11-05 23:33:01 +08:00
回复了 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
2019-08-03 14:44:37 +08:00
回复了 coolair 创建的主题 问与答 Python 字典如何选择几个给定的 key,生成新的字典?
{k: data[k] for k in ('a','b')}
我单位电脑装的趋势杀毒网络版,正常卸载要密码,被我用 360 安全卫士强制把软件安装文件夹给粉碎掉了 23333
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1000 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 22:11 · PVG 06:11 · LAX 15:11 · JFK 18:11
Developed with CodeLauncher
♥ Do have faith in what you're doing.