推导表达式其实就是简化一些循环判断操作等
生成一个数字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 [] >>>