Python 板


LINE

原po的程式码可以说是generator版本的list comprehension, 什麽是list comprehension? 其实把原叙述的左右小括号改成中括号就是了,如下: >>> test = [(x,y) for x in range(3) for y in range(x)] 会产生出类似的结果: >>> test [(1, 0), (2, 0), (2, 1)] test会是一个list。这样子的叙述其实等价於: >>> test = [] >>> for x in range(3): for y in range(x): test.append((x,y)) (从上述程式码原po大概就可以理解为什麽会有那样的output了吧!) list comprehension提供了一个更快速的方式建立起一个有规律的list, x和y也会被清理掉而不会像下面那种方式依然存在。 但原po的程式码则叫做generator expression: >>> test = ((x,y) for x in range(3) for y in range(x)) 得到的test会是一个generator,是一种iterator, 可以透过next()取得下一个值直到没有东西, >>> next(test) (1, 0) >>> next(test) (2, 0) >>> next(test) (2, 1) >>> next(test) Traceback (most recent call last): File "<pyshell#42>", line 1, in <module> next(test) StopIteration 也可以像原po一样透过for回圈取值出来, 所以会得到类似list comprehension的结果。 那样的叙述等价於: >>> def test(): for x in range(3): for y in range(x): yield(x,y) >>> test = test() generator expression省去了建立generator function,实作iterator class的麻烦。 ※ 引述《Neverfor (yorker)》之铭言: : test=( (x, y) for x in range ( 3 ) for y in range (x) ) : for x,y in test: : print(x,y) : output: : 1 0 : 2 0 : 2 1 : 不好意思 看了很久想不出来程式码第一行跟结果的关系QQ : 这种用法是什麽意思呢? : 想问 : 1. (x,y)是否代表 输出的iterator 1个elemeny 是 (x,y) : 2.回圈看不太懂意思 : 前面的for x 是否是後面y range的x --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 118.168.202.245
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1467425395.A.84B.html ※ 编辑: max80713 (118.168.202.245), 07/02/2016 10:14:59 ※ 编辑: max80713 (118.168.202.245), 07/02/2016 10:21:00
1F:推 ccvs: 推07/02 13:37
※ 编辑: max80713 (42.73.86.45), 07/02/2016 14:04:02
2F:推 Neverfor: 感谢大大用心回覆!! 07/02 21:54
3F:推 Neverfor: 请问这种的类似用法,实际上好用吗@@? 07/02 21:56
4F:→ max80713: generator expression好用的地方在於短短的一行程式码就 07/02 22:19
5F:→ max80713: 可以做出你想要的generator;另外它会在需要用到的时候 07/02 22:19
6F:→ max80713: 才分配一块记忆体给下一个值,不像list comprehension 07/02 22:19
7F:→ max80713: 产出越长的list,就会占据越大的记忆体空间。 07/02 22:19
8F:推 Sunal: 当list超大的时候就很好用了 07/02 22:37
9F:推 Neverfor: 谢谢 07/03 03:40
10F:推 Yshuan: 推讲解 07/03 04:18
11F:推 enjoyloli: 所以是处理超大List用小刮号,一般情况用中挂号? 07/03 14:46
12F:→ max80713: 端看需求而定:如果你想call一些list function或是重复 07/03 15:42
13F:→ max80713: 使用这些值那建议把它们存成list;如果只是要扫过所有 07/03 15:42
14F:→ max80713: 值一次,或是个数实在太多就用generator:) 07/03 15:42
15F:推 yougigun: 长知识! 07/04 00:04
16F:推 sunshinehope: 呜呜推 07/04 10:03







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP