Wat doen de functies map(), filter(), reduce()?

Oefenen met coding-interviewvragen in Python

Kirill Smirnov

Data Science Consultant, Altran

map()

map(                                                )
Oefenen met coding-interviewvragen in Python

map()

map(                       Iterable1, Iterable2, ...)

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

Oefenen met coding-interviewvragen in Python

map()

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

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

1, 10, ... $\rightarrow$ function(1, 10, ...) $\rightarrow$ nieuw object

2, 20, ... $\rightarrow$ function(2, 20, ...) $\rightarrow$ nieuw object

3, 30, ... $\rightarrow$ function(3, 30, ...) $\rightarrow$ nieuw object

4, 40, ... $\rightarrow$ function(4, 40, ...) $\rightarrow$ nieuw object

5, 50, ... $\rightarrow$ function(5, 50, ...) $\rightarrow$ nieuw object

Oefenen met coding-interviewvragen in Python

map() met één Iterable

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

Doel: krijg [1, 4, 9, 16, 25]

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

squares is een iterable

for square in squares:
    print(square)
1
4
9
16
25
Oefenen met coding-interviewvragen in Python

map() met één Iterable

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

Doel: krijg [1, 4, 9, 16, 25]

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

squares is een Iterable

list(squares)
[1, 4, 9, 16, 25]
Oefenen met coding-interviewvragen in Python

map() met één Iterable

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

Doel: krijg [1, 4, 9, 16, 25]

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

squares is een Iterator

next(squares)
1
next(squares)
4
Oefenen met coding-interviewvragen in Python

map() met lambda-expressies

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

Doel: krijg [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]

Doel: krijg [1, 4, 9, 16, 25]

squares = map(lambda x: x**2, nums)
list(squares)
[1, 4, 9, 16, 25]
Oefenen met coding-interviewvragen in Python

map() met meerdere Iterables

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

Doel: krijg [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]
Oefenen met coding-interviewvragen in Python

filter()

filter(                     )
Oefenen met coding-interviewvragen in Python

filter()

filter(             Iterable)

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

Oefenen met coding-interviewvragen in Python

filter()

filter(function(x), Iterable)

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

1 $\rightarrow$ function(1) $\rightarrow$ True $\rightarrow$ 1 blijft

2 $\rightarrow$ function(2) $\rightarrow$ False $\rightarrow$ 2 valt af

3 $\rightarrow$ function(3) $\rightarrow$ True $\rightarrow$ 3 blijft

4 $\rightarrow$ function(4) $\rightarrow$ False $\rightarrow$ 4 valt af

5 $\rightarrow$ function(5) $\rightarrow$ True $\rightarrow$ 5 blijft

Oefenen met coding-interviewvragen in Python

filter()-voorbeeld

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

Doel: krijg [1, 2, 3]

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

fobj is een Iterable

for item in fobj:
    print(item)
1
2
3
Oefenen met coding-interviewvragen in Python

filter()-voorbeeld

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

Doel: krijg [1, 2, 3]

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

fobj is een Iterable

list(fobj)
[1, 2, 3]
Oefenen met coding-interviewvragen in Python

filter()-voorbeeld

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

Doel: krijg [1, 2, 3]

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

fobj is een Iterator

next(fobj)
1
next(fobj)
4
Oefenen met coding-interviewvragen in Python

filter() met lambda-expressies

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

Doel: krijg [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]

Doel: krijg [1, 2, 3]

fobj = filter(lambda x: x > 0, nums)
list(fobj)
[1, 2, 3]
Oefenen met coding-interviewvragen in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ nieuw object van hetzelfde type als de inhoud

De weergave van de gegeven lijst

Oefenen met coding-interviewvragen in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ nieuw object van hetzelfde type als de inhoud

De eerste twee elementen in de lijst combineren

Oefenen met coding-interviewvragen in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ nieuw object van hetzelfde type als de inhoud

De uitvoer combineren met het derde element in de lijst

Oefenen met coding-interviewvragen in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ nieuw object van hetzelfde type als de inhoud

De uitvoer combineren met het vierde element in de lijst

Oefenen met coding-interviewvragen in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ nieuw object van hetzelfde type als de inhoud

De uitvoer combineren met het vijfde element in de lijst

Oefenen met coding-interviewvragen in Python

reduce()-voorbeeld

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

Doel: krijg 1 - minimum

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 - eindresultaat

Oefenen met coding-interviewvragen in Python

reduce() met lambda-expressies

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

Doel: krijg 1 - minimum

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

Doel: krijg 1 - minimum

reduce(lambda x, y: x if x < y else y, nums)
1
Oefenen met coding-interviewvragen in Python

Laten we oefenen!

Oefenen met coding-interviewvragen in Python

Preparing Video For Download...