map(), filter(), reduce() とは?

Pythonでコーディング面接問題を練習する

Kirill Smirnov

Data Science Consultant, Altran

map()

map(                                                )
Pythonでコーディング面接問題を練習する

map()

map(                       Iterable1, Iterable2, ...)

Iterable: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...

Pythonでコーディング面接問題を練習する

map()

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

Iterable: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...

1, 10, ... → function(1, 10, ...) → 新しいオブジェクト

2, 20, ... → function(2, 20, ...) → 新しいオブジェクト

3, 30, ... → function(3, 30, ...) → 新しいオブジェクト

4, 40, ... → function(4, 40, ...) → 新しいオブジェクト

5, 50, ... → function(5, 50, ...) → 新しいオブジェクト

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 は iterable

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 は Iterable

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)

Iterable: [1, 2, 3, 4, 5]

Pythonでコーディング面接問題を練習する

filter()

filter(function(x), Iterable)

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 は Iterable

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 は Iterable

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)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → 要素と同じ型の新しいオブジェクト

与えられたリストの表現

Pythonでコーディング面接問題を練習する

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → 要素と同じ型の新しいオブジェクト

リストの最初の2要素を結合

Pythonでコーディング面接問題を練習する

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → 要素と同じ型の新しいオブジェクト

出力とリストの3番目の要素を結合

Pythonでコーディング面接問題を練習する

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → 要素と同じ型の新しいオブジェクト

出力とリストの4番目の要素を結合

Pythonでコーディング面接問題を練習する

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] → 要素と同じ型の新しいオブジェクト

出力とリストの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) → 4

smallest(4, 5) → 4

smallest(4, 1) → 1

smallest(1, 9) → 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でコーディング面接問題を練習する

Passons à la pratique !

Pythonでコーディング面接問題を練習する

Preparing Video For Download...