一、解析式是什麼?爲何要用解析式?python
對一個list裏全部的數值求平方express
In [123]: lst = list(range(6)) In [124]: ret = [] In [125]: for i in lst: ...: ret.append(i ** 2) ...: In [126]: ret Out[126]: [0, 1, 4, 9, 16, 25]
解析式:bash
In [127]: lst = list(range(6)) In [128]: [ x ** 2 for x in lst ] # 列表解析 Out[128]: [0, 1, 4, 9, 16, 25]
求list中偶數的平方
app
In [137]: lst Out[137]: [0, 1, 2, 3, 4, 5] In [138]: for i in lst: ...: if i % 2 == 0: ...: print(i ** 2) ...: ...: 0 4 16 In [140]: [ i ** 2 for i in lst if i % 2 == 0 ] Out[140]: [0, 4, 16]
列表解析的語法格式:
ide
[ expr for i in iterable ]spa
[ expr for i in iterable if子句 ]ip
解析式代碼簡潔,可讀性高;效率比普通迭代稍高input
if子句能夠出現屢次:generator
In [143]: [ i ** 2 for i in lst if i % 2 == 0 if i > 0] Out[143]: [4, 16] In [151]: [ i ** 2 for i in lst if i % 2 == 0 and i > 0 and i < 4 ] Out[151]: [4] In [149]: [ i ** 2 for i in lst if i % 2 == 0 if i > 0 or i < 4 ] Out[149]: [0, 4, 16]
帶多個if語句的均可以轉化爲條件的邏輯運算,因此通常不會帶多個if語句
it
for語句也能夠有多個: # 就是嵌套迭代
In [153]: [ x, y for x in range(5) for y in range(5, 10) ] File "<ipython-input-153-8aadadd13ebf>", line 1 [ x, y for x in range(5) for y in range(5, 10) ] ^ SyntaxError: invalid syntax In [154]: [ (x, y) for x in range(5) for y in range(5, 10) ] # 表達式只能有一個 Out[154]: [(0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)] In [155]: In [161]: [ (x, y) for x in range(5) if x > 2 for y in range(5, 10) ] Out[161]: [(3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)] In [159]: [ (x, y) for x in range(5) for y in range(5, 10) if x > 2 ] Out[159]: [(3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9)]
解析式也能夠嵌套:
In [163]: [ x ** 2 for x in [ x for x in [1, 2, 3] ] ] Out[163]: [1, 4, 9]
一眼看不出解析式的結果是何時,就不要用解析式
二、三元表達式
expr1 if 條件子句 else expr2
當條件蠻族時,返回expr1 或則返回expr2
In [165]: x = 3 In [166]: x ** 2 if x % 2 == 0 else x ** 3 Out[166]: 27 In [172]: name = "xxj" if 3 > 0 else "xj" In [173]: name Out[173]: 'xxj' In [174]: name = "xxj" if 3 <0 else "xj" In [175]: name Out[175]: 'xj' In [176]: name = "xxj" if 3 > 0 else name = "xj" File "<ipython-input-176-36d6075bedd6>", line 1 name = "xxj" if 3 > 0 else name = "xj" ^ SyntaxError: can't assign to conditional expression
列表解析和三元表達式結合:
In [180]: [ x ** 2 if x % 2 == 0 else x ** 3 for x in [1, 2, 3, 4 ] ] Out[180]: [1, 4, 27, 16] In [178]: [ x ** 2 for x in [ 1, 2, 3, 4 ] if x % 2 == 0 else x ** 3 ] File "<ipython-input-178-63a49a9bdf59>", line 1 [ x ** 2 for x in [ 1, 2, 3, 4 ] if x % 2 == 0 else x ** 3 ] ^ SyntaxError: invalid syntax
三、生成器解析
列表解析返回的是list
In [8]: ( x ** 2 for x in range(1000) ) Out[8]: <generator object <genexpr> at 0x7f770e8eeb48> In [9]: g = ( x ** 2 for x in range(1000) ) In [10]: type(g) Out[10]: generator In [11]: help(g) In [12]: g.__next__ Out[12]: <method-wrapper '__next__' of generator object at 0x7f770de47b48> In [13]: g.__next__() Out[13]: 0 In [14]: g.__next__() Out[14]: 1 In [15]: g.__next__() Out[15]: 4 In [16]: next(g) Out[16]: 9 In [17]: next(g) Out[17]: 16
生成器解析返回一個生成器
生成器在取值的時候才計算
生成器解析式和列表解析式除了中括號改爲小括號其它的徹底同樣
四、集合解析式
集合解析返回一個集合
集合解析式和列表解析式除了中括號改爲小括號其它的徹底同樣
In [24]: [ x ** 2 for x in range(5) ] Out[24]: [0, 1, 4, 9, 16] In [25]: ( x ** 2 for x in range(5) ) Out[25]: <generator object <genexpr> at 0x7f770dd5c7d8> In [27]: { x ** 2 for x in range(5) } Out[27]: {0, 1, 4, 9, 16}
五、字典解析式
字典解析返回一個集合
In [28]: { str(x):x ** 2 for x in range(5) } Out[28]: {'0': 0, '1': 1, '2': 4, '3': 9, '4': 16} In [32]: { str(x + 1):x ** 2 for x in range(5) } Out[32]: {'1': 0, '2': 1, '3': 4, '4': 9, '5': 16} In [46]: { k:v for k, v in [('a', 1), ('b', 2)] } Out[46]: {'a': 1, 'b': 2}