推导式

推导式:comprehensions(又称解析式),是Python的一种独有特性,相当于语法糖的存在,推导式是可以从一个数据序列构建另一个新的数据序列的结构体。

共有三种推导,在Python2和3中都有支持:

  • 列表(list)推导式
  • 集合(set)推导式
  • 字典(dict)推导式

列表推导式 randint

基本格式: 变量名 = [expression1 for variable in sequence[, if expression2]]

expression1 : 列表生成元素表达式,可以是有返回值的函数。

for variable in sequence : 迭代sequence将variable传入expression1。

if expresiion2 : 可选,根据条件过滤。

 

1.最基本的列表推导式

>>> arr = [i for i in range(5)]
>>> arr
[0, 1, 2, 3, 4]

2.附带运算的列表推导式

>>> arr = [i**2 for i in range(5)]
>>> arr
[0, 1, 4, 9, 16]
>>> arr = [pow(i, 2) for i in range(5)]
>>> arr
[0, 1, 4, 9, 16]
>>> arr = [pow(i, 2) if i%2==0 else -pow(i, 2) for i in range(5)]
>>> arr
[0, -1, 4, -9, 16]

3.附带条件的列表推导式

>>> arr = [i for i in range(10) if i < 5]
>>> arr
[0, 1, 2, 3, 4]

4.多重循环的列表推导式

>>> list_ = [[1, 2], [3, 4], [5, 6]]
>>> arr = [i for sub_list in list_ for i in sub_list]
>>> arr
[1, 2, 3, 4, 5, 6]
>>> arr = [x+y for x in range(5) for y in range(2)]
>>> arr
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5]
# 0 1 2 3 4
# 0 1 0 1 0 1 0 1 0 1
# 0 1 1 2 2 3 3 4 4 5

5.更多用法

>>> dict_ = {'P':'y', 't':'h', 'o':'n'}
>>> arr = [k+v for k,v in dict_.items()]
>>> arr
['Py', 'th', 'on']
>>> from random import randint
>>> arr = [randint(1, 5) for i in range(5)]
>>> arr
[3, 2, 3, 2, 1]

集合推导式

与列表推导式差不多一样,只不过是将[ ]换为{ }

set_ = {x+y for x in range(5) for y in range(3)}
>>> set_
{0, 1, 2, 3, 4, 5, 6}
>>> set_ = {k+v for k,v in dict_.items()}
>>> set_
{'on', 'Py', 'th'}

字典推导式

变量名 = {表达式 for 临时变量 in 可迭代内容}

 

>>> new_dict = {i:pow(i, 2) for i in range(5)}
>>> new_dict
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> new_dict = {x:'Python'.index(x) for x in 'Python'}
>>> new_dict
{'P': 0, 'y': 1, 't': 2, 'h': 3, 'o': 4, 'n': 5}
>>>
>>> dict_ = {'P':'y', 't':'h', 'o':'n'}
>>> new_dict = {v:k for k,v in dict_.items()}
>>> new_dict
{'y': 'P', 'h': 't', 'n': 'o'}
>>> dict_ = {'a': 10, 'b': 20, 'A': 3, 'C': 7, 'd': 5, 'x': 2, 'X': 4, 'y': 8}
>>> new_dict = {k.upper(): dict_.get(k.lower(), 0) + dict_.get(k.upper(), 0) for k in dict_.keys() if k in 'aAbcDxy'}
>>> new_dict
{'A': 13, 'B': 20, 'X': 6, 'Y': 8}

为什么元组不可以用推导式

Python中,将( )用于了生成器,将列表推导式的[ ]改成( )即可得到生成器。

>>> gener = (i for i in range(5))
>>> type(gener)
<class 'generator'>
>>> gener
<generator object <genexpr> at 0x00000179ECB01BA0>
>>> [i for i in gener]
[0, 1, 2, 3, 4]
>>> [i for i in gener]
[]

不过可以通过类型强制性转换,将生成器转换为对应的类型数据

>>> gener = (i for i in range(5))
>>> list(gener)
[0, 1, 2, 3, 4]
>>> gener = (i for i in range(5))
>>> tuple(gener)
(0, 1, 2, 3, 4)
>>> gener = (i for i in range(5))
>>> set(gener)
{0, 1, 2, 3, 4}
05-21 22:26