每次看大牛的代码很奇怪,比如T=[(x,y) for x in range(5) if x%2==0 for y in range(5) if y %2==1]
这是什么鬼,一个变量T为什么搞得这么拥挤,看着也累啊,后来查了一下才知道这个叫推导式。
推导式是可以从一个数据序列构建另一个新的数据序列的结构体。
列表推导式
列表推导能非常简洁的构造一个新列表:只用一条简洁的表达式即可对得到的元素进行转换变形
其基本格式如下:
代码如下:[expression for value in collection if condition]
过滤条件可有可无,取决于实际应用,只留下表达式;相当于下面这段for循环:
代码如下:
result = []
for value in collection:
if condition:
result.append(expression)
例1: 过滤掉长度小于3的字符串列表,并将剩下的转换成大写字母
代码如下:
>>> names = ['bob','tom','alice','jerry','wendy','smith']
>>> [name.upper() for name in names if len(name)>3]
['ALICE', 'JERRY', 'WENDY', 'SMITH']
例2: 求(x,y)其中x是0-5之间的偶数,y是0-5之间的奇数组成的元祖列表
代码如下:
>>> [(x,y) for x in range(5) if x%2==0 for y in range(5) if y %2==1]
[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]
例3: 求m中3,6,9组成的列表
代码如下:
>>> m = [[1,2,3],
... [4,5,6],
... [7,8,9]]
>>> m
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [row[2] for row in m]
[3, 6, 9]
#或者用下面的方式
>>> [m[row][2] for row in (0,1,2)]
[3, 6, 9]
例4: 求m中斜线1,5,9组成的列表
代码如下:
>>> m
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [m[i][i] for i in range(len(m))]
[1, 5, 9]
例5: 求m,n中矩阵和元素的乘积
代码如下:
>>> m = [[1,2,3],
... [4,5,6],
... [7,8,9]]
>>> n = [[2,2,2],
... [3,3,3],
... [4,4,4]]
>>> [m[row][col]*n[row][col] for row in range(3) for col in range(3)]
[2, 4, 6, 12, 15, 18, 28, 32, 36]
>>> [[m[row][col]*n[row][col] for col in range(3)] for row in range(3)]
[[2, 4, 6], [12, 15, 18], [28, 32, 36]]
>>> [[m[row][col]*n[row][col] for row in range(3)] for col in range(3)]
[[2, 12, 28], [4, 15, 32], [6, 18, 36]]
例5: 讲字典中age键,按照条件赋新值
代码如下:
>>> bob
{'pay': 3000, 'job': 'dev', 'age': 42, 'name': 'bob smith'}
>>> sue
{'pay': 4000, 'job': 'hdw', 'age': 45, 'name': 'sue jones'}
>>> people = [bob, sue]
>>> [rec['age']+100 if rec['age'] >= 45 else rec['age'] for rec in people] # 注意for位置
[42, 145]
等同于
>>> age = []
>>> for rec in people
... if rec['age'] >=45
... age.append(rec['age']+100)
... else
... age.append(rec['age'])
>>>age
例6,如下的列表推导式结合两个列表的元素,如果元素之间不相等的话:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
等同于:
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x != y:
... combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
值得注意的是在上面两个方法中的 for 和 if 语句的顺序。
如果想要得到一个元组 (例如,上面例子中的 (x, y)),必须要加上括号:
>>> vec = [-4, -2, 0, 2, 4]
>>> # create a new list with the values doubled
>>> [x*2 for x in vec]
[-8, -4, 0, 4, 8]
>>> # filter the list to exclude negative numbers
>>> [x for x in vec if x >= 0]
[0, 2, 4]
>>> # apply a function to all the elements
>>> [abs(x) for x in vec]
[4, 2, 0, 2, 4]
>>> # call a method on each element
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> # create a list of 2-tuples like (number, square)
>>> [(x, x**2) for x in range(6)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> # the tuple must be parenthesized, otherwise an error is raised
>>> [x, x**2 for x in range(6)]
File "", line 1, in ?
[x, x**2 for x in range(6)]
^
SyntaxError: invalid syntax
>>> # flatten a list using a listcomp with two 'for'
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
列表推导式可使用复杂的表达式和嵌套函数:
>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
列表推导式可以嵌套。
考虑以下的 3x4 矩阵,一个列表中包含三个长度为4的列表:
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
现在,如果你想交换行和列,可以用嵌套的列表推导式:
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
像前面看到的,嵌套的列表推导式是对 for 后面的内容进行求值,所以上例就等价于:
>>> transposed = []
>>> for i in range(4):
... transposed.append([row[i] for row in matrix])
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
反过来说,如下也是一样的:
>>> transposed = []
>>> for i in range(4):
... # the following 3 lines implement the nested listcomp
... transposed_row = []
... for row in matrix:
... transposed_row.append(row[i])
... transposed.append(transposed_row)
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
在实际中,你应该更喜欢使用内置函数组成复杂流程语句。对此种情况 zip() 函数将会做的更好:
>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
字典推导式
字典和集合推导式是该思想的延续,语法差不多,只不过产生的是集合和字典而已。其基本格式如下:
代码如下:{ key_expr: value_expr for value in collection if condition }
例1: 用字典推导式以字符串以及其所在位置建字典
代码如下:
>>> strings = ['import','is','with','if','file','exception']
>>> d = {key: val for val,key in enumerate(strings)}
>>> d
{'import': 0, 'is': 1, 'with': 2, 'if': 3, 'file': 4, 'exception': 5}
集合推导式
集合推导式跟列表推导式非常相似,唯一区别在于用{}代替[]。其基本格式如下:
代码如下:
{ expr for value in collection if condition }
例1: 用集合推导建字符串长度的集合
代码如下:
>>> strings = ['a','is','with','if','file','exception']
>>> {len(s) for s in strings} #有长度相同的会只留一个,这在实际上也非常有用
set([1, 2, 4, 9])
嵌套列表推导式
嵌套列表是指列表中嵌套列表,比如说:
>>> l = [[1,2,3],
[4,5,6],
[7,8,9]]
例1: 一个由男人列表和女人列表组成的嵌套列表,取出姓名中带有两个以上字母e的姓名,组成列表
代码如下:
names = [['tom','billy','jefferson','andrew','wesley','steven','joe'],
['alice','jill','ana','wendy','jennifer','sherry','eva']]
用for循环实现:
代码如下:
>>>tmp = []
>>>for lst in names:
... for name in lst:
... if name.count('e') >= 2:
... tmp.append(name)
>>>tmp
['jefferson', 'wesley', 'steven', 'jennifer']
用嵌套列表实现:
代码如下:
>>> names = [['tom','billy','jefferson','andrew','wesley','steven','joe'],
['alice','jill','ana','wendy','jennifer','sherry','eva']]
>>> [name for lst in names for name in lst if name.count('e')>=2] #注意遍历顺序,这是实现的关键
['jefferson', 'wesley', 'steven', 'jennifer']