什麼是函式 map()、filter()、reduce()?

Python 程式面試題實作練習

Kirill Smirnov

Data Science Consultant, Altran

map()

map(                                                )
Python 程式面試題實作練習

map()

map(                       Iterable1, Iterable2, ...)

可疊代物件:[1, 2, 3, 4, 5][10, 20, 30, 40, 50]、...

Python 程式面試題實作練習

map()

map(function(x1, x2, ...), Iterable1, Iterable2, ...)

可疊代物件:[1, 2, 3, 4, 5][10, 20, 30, 40, 50]、...

110 … $\rightarrow$ function(1, 10, ...) $\rightarrow$ 新物件

220 … $\rightarrow$ function(2, 20, ...) $\rightarrow$ 新物件

330 … $\rightarrow$ function(3, 30, ...) $\rightarrow$ 新物件

440 … $\rightarrow$ function(4, 40, ...) $\rightarrow$ 新物件

550 … $\rightarrow$ function(5, 50, ...) $\rightarrow$ 新物件

Python 程式面試題實作練習

單一 Iterable 的 map()

nums = [1, 2, 3, 4, 5]

目標輸出 [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>

squares 是可疊代物件

for square in squares:
    print(square)
1
4
9
16
25
Python 程式面試題實作練習

單一 Iterable 的 map()

nums = [1, 2, 3, 4, 5]

目標輸出 [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>

squares 是可疊代物件

list(squares)
[1, 4, 9, 16, 25]
Python 程式面試題實作練習

單一 Iterable 的 map()

nums = [1, 2, 3, 4, 5]

目標輸出 [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>

squares 是反覆器(Iterator)

next(squares)
1
next(squares)
4
Python 程式面試題實作練習

以 lambda 搭配 map()

nums = [1, 2, 3, 4, 5]

目標輸出 [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
list(squares)
[1, 4, 9, 16, 25]
nums = [1, 2, 3, 4, 5]

目標輸出 [1, 4, 9, 16, 25]

squares = map(lambda x: x**2, nums)
list(squares)
[1, 4, 9, 16, 25]
Python 程式面試題實作練習

多個 Iterable 的 map()

nums1 = [1, 2, 3, 4, 5]
nums2 = [10, 20, 30, 40, 50]

目標輸出:[1*10, 2*20, 3*30, 4*40, 5*50] = [10, 40, 90, 160, 250]

mult = map(lambda x, y: x*y, nums1, nums2)
list(mult)
[10, 40, 90, 160, 250]
Python 程式面試題實作練習

filter()

filter(                     )
Python 程式面試題實作練習

filter()

filter(             Iterable)

可疊代物件:[1, 2, 3, 4, 5]

Python 程式面試題實作練習

filter()

filter(function(x), Iterable)

可疊代物件:[1, 2, 3, 4, 5]

1 $\rightarrow$ function(1) $\rightarrow$ True $\rightarrow$ 保留 1

2 $\rightarrow$ function(2) $\rightarrow$ False $\rightarrow$ 移除 2

3 $\rightarrow$ function(3) $\rightarrow$ True $\rightarrow$ 保留 3

4 $\rightarrow$ function(4) $\rightarrow$ False $\rightarrow$ 移除 4

5 $\rightarrow$ function(5) $\rightarrow$ True $\rightarrow$ 保留 5

Python 程式面試題實作練習

filter() 範例

nums = [-3, -2, -1, 0, 1, 2, 3]

目標輸出:[1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>

fobj 是可疊代物件

for item in fobj:
    print(item)
1
2
3
Python 程式面試題實作練習

filter() 範例

nums = [-3, -2, -1, 0, 1, 2, 3]

目標輸出:[1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>

fobj 是可疊代物件

list(fobj)
[1, 2, 3]
Python 程式面試題實作練習

filter() 範例

nums = [-3, -2, -1, 0, 1, 2, 3]

目標輸出:[1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>

fobj 是反覆器(Iterator)

next(fobj)
1
next(fobj)
4
Python 程式面試題實作練習

以 lambda 搭配 filter()

nums = [-3, -2, -1, 0, 1, 2, 3]

目標輸出:[1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
list(fobj)
[1, 2, 3]
nums = [-3, -2, -1, 0, 1, 2, 3]

目標輸出:[1, 2, 3]

fobj = filter(lambda x: x > 0, nums)
list(fobj)
[1, 2, 3]
Python 程式面試題實作練習

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

可疊代物件:[1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ 產生與內容型別相同的新物件

給定清單的示意圖

Python 程式面試題實作練習

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

可疊代物件:[1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ 產生與內容型別相同的新物件

合併清單中的前兩個元素

Python 程式面試題實作練習

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

可疊代物件:[1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ 產生與內容型別相同的新物件

將前一步的輸出與清單第 3 個元素合併

Python 程式面試題實作練習

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

可疊代物件:[1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ 產生與內容型別相同的新物件

將輸出與清單第 4 個元素合併

Python 程式面試題實作練習

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

可疊代物件:[1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ 產生與內容型別相同的新物件

將輸出與清單第 5 個元素合併

Python 程式面試題實作練習

reduce() 範例

nums = [8, 4, 5, 1, 9]

目標輸出:1(最小值)

def smallest(x, y):
    if x < y:
        return x
    else:
        return y
reduce(smallest, nums)
1

smallest(8, 4) $\rightarrow$ 4

smallest(4, 5) $\rightarrow$ 4

smallest(4, 1) $\rightarrow$ 1

smallest(1, 9) $\rightarrow$ 1(最終結果)

Python 程式面試題實作練習

以 lambda 搭配 reduce()

nums = [8, 1, 4, 2, 9]

目標輸出:1(最小值)

def smallest(x, y):
    if x < y:
        return x
    else:
        return y
reduce(smallest, nums)
1
nums = [8, 1, 4, 2, 9]

目標輸出:1(最小值)

reduce(lambda x, y: x if x < y else y, nums)
1
Python 程式面試題實作練習

一起來練習吧!

Python 程式面試題實作練習

Preparing Video For Download...