forked from ethan-funny/explore-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_reduce_filter.md
More file actions
131 lines (90 loc) · 3.21 KB
/
Copy pathmap_reduce_filter.md
File metadata and controls
131 lines (90 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# map/reduce/filter
map/reduce/filter 是 Python 中较为常用的内建高阶函数,它们为函数式编程提供了不少便利。
# map
`map` 函数的使用形式如下:
```
map(function, sequence)
```
**解释**:对 sequence 中的 item 依次执行 function(item),并将结果组成一个 List 返回,也就是:
```
[function(item1), function(item2), function(item3), ...]
```
看一些简单的例子。
```python
>>> def square(x):
... return x * x
>>> map(square, [1, 2, 3, 4])
[1, 4, 9, 16]
>>> map(lambda x: x * x, [1, 2, 3, 4]) # 使用 lambda
[1, 4, 9, 16]
>>> map(str, [1, 2, 3, 4])
['1', '2', '3', '4']
>>> map(int, ['1', '2', '3', '4'])
[1, 2, 3, 4]
```
再看一个例子:
```python
def double(x):
return 2 * x
def triple(x):
return 3 *x
def square(x):
return x * x
funcs = [double, triple, square] # 列表元素是函数对象
# 相当于 [double(4), triple(4), square(4)]
value = list(map(lambda f: f(4), funcs))
print value
# output
[8, 12, 16]
```
上面的代码中,我们加了 list 转换,是为了兼容 Python3,在 Python2 中 map 直接返回列表,Python3 中返回迭代器。
# reduce
`reduce` 函数的使用形式如下:
```
reduce(function, sequence[, initial])
```
**解释**:先将 sequence 的前两个 item 传给 function,即 function(item1, item2),函数的返回值和 sequence 的下一个 item 再传给 function,即 function(function(item1, item2), item3),如此迭代,直到 sequence 没有元素,如果有 initial,则作为初始值调用。
也就是说:
```
reduece(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
```
看一些例子,就能很快理解了。
```python
>>> reduce(lambda x, y: x * y, [1, 2, 3, 4]) # 相当于 ((1 * 2) * 3) * 4
24
>>> reduce(lambda x, y: x * y, [1, 2, 3, 4], 5) # ((((5 * 1) * 2) * 3)) * 4
120
>>> reduce(lambda x, y: x / y, [2, 3, 4], 72) # (((72 / 2) / 3)) / 4
3
>>> reduce(lambda x, y: x + y, [1, 2, 3, 4], 5) # ((((5 + 1) + 2) + 3)) + 4
15
>>> reduce(lambda x, y: x - y, [8, 5, 1], 20) # ((20 - 8) - 5) - 1
6
>>> f = lambda a, b: a if (a > b) else b # 两两比较,取最大值
>>> reduce(f, [5, 8, 1, 10])
10
```
# filter
`filter` 函数用于过滤元素,它的使用形式如下:
```
filter(function, sequnce)
```
**解释**:将 function 依次作用于 sequnce 的每个 item,即 function(item),将返回值为 True 的 item 组成一个 List/String/Tuple (取决于 sequnce 的类型,python3 统一返回迭代器) 返回。
看一些例子。
```python
>>> even_num = list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6]))
>>> even_num
[2, 4, 6]
>>> odd_num = list(filter(lambda x: x % 2, [1, 2, 3, 4, 5, 6]))
>>> odd_num
[1, 3, 5]
>>> filter(lambda x: x < 'g', 'hijack')
'ac' # python2
>>> filter(lambda x: x < 'g', 'hijack')
<filter object at 0x1034b4080> # python3
```
# 小结
- map/reduce/filter 为函数式编程提供了不少便利,可使代码变得更简洁;
- 注意在 python2 和 python3 中,map/reduce/filter 的返回值类型有所不同,python2 返回的是基本数据类型,而 python3 则返回了迭代器;
# 参考资料
- [Python Tutorial: Lambda Operator, filter, reduce and map](http://www.python-course.eu/lambda.php)