python中强大优雅的列表推导表达式
2018-06-18 02:04:49来源:未知 阅读 ()
推导表达式其实就是简化一些循环判断操作等
生成一个数字1-10的列表,可以有多少种方法?
>>> l = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] >>> l [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
>>> l = [] >>> for x in range( 1, 11 ): ... l.append( x ) ... >>> l [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
>>> l = range( 1, 11 ) >>> l [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
现在看下推导表达式
>>> a = [ x for x in range( 1, 11 ) ] >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
有些人,可能会说,直接range( 1, 11 )就好了,多此一举,如果我们要筛选出奇数?
当然,range依然能够做到:
>>> range( 1, 11, 2 ) [1, 3, 5, 7, 9] >>>
那,如果要得到偶数,需要遍历每一项,判断
>>> a = [] >>> for x in range( 1, 11 ): ... if x % 2 == 0: ... a.append( x ) ... >>> >>> a [2, 4, 6, 8, 10] >>>
他等价于如下的推导表达式:
>>> b = [ x for x in range( 1, 11 ) if x % 2 == 0 ] >>> b [2, 4, 6, 8, 10] >>>
一句话搞定
生成一个坐标系?
>>> dot = [(x,y) for x in range( 1, 10 ) for y in range( 1, 10 ) ] >>> dot [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)] >>>
等价于,如下2重循环:
>>> dot = [] >>> for x in range( 1, 10 ): ... for y in range( 1, 10 ): ... dot.append( ( x, y ) ) ... >>> dot [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9)] >>>
生成一个递增字符串列表:
>>> ['the number:%s' % n for n in range( 1, 10 ) ] ['the number:1', 'the number:2', 'the number:3', 'the number:4', 'the number:5', 'the number:6', 'the number:7', 'the number:8', 'the number:9']
求1-9每个数的平方
>>> [x ** 2 for x in range( 1, 10 ) ] [1, 4, 9, 16, 25, 36, 49, 64, 81] >>>
用字典打包一层,相同的键后面会覆盖前面的
>>> dict( [( x, y ) for x in range( 1, 5 ) for y in range( 1, 5 )] ) {1: 4, 2: 4, 3: 4, 4: 4} >>> [( x, y ) for x in range( 1, 5 ) for y in range( 1, 5 )] [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
列表的引用,跟javascript类型:
>>> l = ['my', 'name', 'is', 'ghostwu' ] >>> l ['my', 'name', 'is', 'ghostwu'] >>> a = l >>> a[3] = 'wukong' >>> l ['my', 'name', 'is', 'wukong'] >>> del a >>> l ['my', 'name', 'is', 'wukong'] >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>>
del a,删除的是 列表的引用,跟php垃圾回收机制类似,两个变量指向一个列表,删除其中一个,但是另一个还是指向那个列表.
>>> a = [10, 20, 30 ] >>> b = a >>> b [10, 20, 30] >>> del a >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> b [10, 20, 30] >>>
del a[], 这个指的是清空列表
>>> a = [ 10, 20, 30 ] >>> b = a >>> del a[:] >>> >>> a [] >>> b [] >>>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash