推導表達式其實就是簡化一些迴圈判斷操作等 生成一個數字1-10的列表,可以有多少種方法? 現在看下推導表達式 有些人,可能會說,直接range( 1, 11 )就好了,多此一舉,如果我們要篩選出奇數? 當然,range依然能夠做到: 那,如果要得到偶數,需要遍歷每一項,判斷 他等價於如下的推導表達式 ...
推導表達式其實就是簡化一些迴圈判斷操作等
生成一個數字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 [] >>>