Python推导式(列表、字典、集合推导式)实例讲解
2018-04-16 10:39:42         来源：xu_xuekai的博客

1.列表推导式

1.1 使用[]生成List

```[out_exp_res for out_exp in input_list if out_exp == 2]
```

out_exp_res:　　列表生成元素表达式，可以是有返回值的函数。for out_exp in input_list：　　迭代input_list将out_exp传入out_exp_res表达式中。if out_exp == 2：　　根据条件过滤哪些值可以。

```In [1]: [x for x in range(10)]
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [2]: [x for x in range(10) if x % 2 ==0]
Out[2]: [0, 2, 4, 6, 8]
```

```In [3]: def f(x):
...:     return x * x
...:

In [4]: [f(x) for x in range(10)]
Out[4]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
```
1.2 使用()生成generator

```In [5]: list = (x for x in range(10) if x % 2 ==0)

In [6]: list
Out[6]:  at 0x0000018C86C88FC0>

In [7]: for i in list:
...:     print(i)
...:
0
2
4
6
8
```

2. 字典推导式

```In [10]: dic = {'a':10, 'b':20}

In [11]: n_dic = {v:k for k,v in dic.items()}

In [12]: n_dic
Out[12]: {10: 'a', 20: 'b'}
```

3. 集合推导式

```In [13]: set1 = {x for x in range(10)}

In [14]: set1
Out[14]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}```