Ce sunt funcțiile map(), filter(), reduce()?

Exersarea întrebărilor de interviu de programare în Python

Kirill Smirnov

Data Science Consultant, Altran

map()

map(                                                )
Exersarea întrebărilor de interviu de programare în Python

map()

map(                       Iterable1, Iterable2, ...)

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

Exersarea întrebărilor de interviu de programare în Python

map()

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

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

1, 10, ... $\rightarrow$ function(1, 10, ...) $\rightarrow$ obiect nou

2, 20, ... $\rightarrow$ function(2, 20, ...) $\rightarrow$ obiect nou

3, 30, ... $\rightarrow$ function(3, 30, ...) $\rightarrow$ obiect nou

4, 40, ... $\rightarrow$ function(4, 40, ...) $\rightarrow$ obiect nou

5, 50, ... $\rightarrow$ function(5, 50, ...) $\rightarrow$ obiect nou

Exersarea întrebărilor de interviu de programare în Python

map() cu un singur Iterabil

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

Scopul este să obținem [1, 4, 9, 16, 25]

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

squares este iterabil

for square in squares:
    print(square)
1
4
9
16
25
Exersarea întrebărilor de interviu de programare în Python

map() cu un singur Iterabil

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

Scopul este să obținem [1, 4, 9, 16, 25]

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

squares este Iterabil

list(squares)
[1, 4, 9, 16, 25]
Exersarea întrebărilor de interviu de programare în Python

map() cu un singur Iterabil

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

Scopul este să obținem [1, 4, 9, 16, 25]

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

squares este Iterator

next(squares)
1
next(squares)
4
Exersarea întrebărilor de interviu de programare în Python

map() cu expresii lambda

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

Scopul este să obținem [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]

Scopul este să obținem [1, 4, 9, 16, 25]

squares = map(lambda x: x**2, nums)
list(squares)
[1, 4, 9, 16, 25]
Exersarea întrebărilor de interviu de programare în Python

map() cu mai multe Iterabile

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

Scopul este să obținem: [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]
Exersarea întrebărilor de interviu de programare în Python

filter()

filter(                     )
Exersarea întrebărilor de interviu de programare în Python

filter()

filter(             Iterable)

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

Exersarea întrebărilor de interviu de programare în Python

filter()

filter(function(x), Iterable)

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

1 $\rightarrow$ function(1) $\rightarrow$ True $\rightarrow$ 1 este păstrat

2 $\rightarrow$ function(2) $\rightarrow$ False $\rightarrow$ 2 este respins

3 $\rightarrow$ function(3) $\rightarrow$ True $\rightarrow$ 3 este păstrat

4 $\rightarrow$ function(4) $\rightarrow$ False $\rightarrow$ 4 este respins

5 $\rightarrow$ function(5) $\rightarrow$ True $\rightarrow$ 5 este păstrat

Exersarea întrebărilor de interviu de programare în Python

Exemplu filter()

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

Scopul este să obținem: [1, 2, 3]

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

fobj este Iterabil

for item in fobj:
    print(item)
1
2
3
Exersarea întrebărilor de interviu de programare în Python

Exemplu filter()

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

Scopul este să obținem: [1, 2, 3]

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

fobj este Iterabil

list(fobj)
[1, 2, 3]
Exersarea întrebărilor de interviu de programare în Python

Exemplu filter()

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

Scopul este să obținem: [1, 2, 3]

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

fobj este Iterator

next(fobj)
1
next(fobj)
4
Exersarea întrebărilor de interviu de programare în Python

filter() cu expresii lambda

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

Scopul este să obținem: [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]

Scopul este să obținem: [1, 2, 3]

fobj = filter(lambda x: x > 0, nums)
list(fobj)
[1, 2, 3]
Exersarea întrebărilor de interviu de programare în Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ obiect nou de același tip ca elementele

Reprezentarea listei date

Exersarea întrebărilor de interviu de programare în Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ obiect nou de același tip ca elementele

Combinarea primelor două elemente din listă

Exersarea întrebărilor de interviu de programare în Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ obiect nou de același tip ca elementele

Combinarea rezultatului cu al treilea element din listă

Exersarea întrebărilor de interviu de programare în Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ obiect nou de același tip ca elementele

Combinarea rezultatului cu al patrulea element din listă

Exersarea întrebărilor de interviu de programare în Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ obiect nou de același tip ca elementele

Combinarea rezultatului cu al cincilea element din listă

Exersarea întrebărilor de interviu de programare în Python

Exemplu reduce()

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

Scopul este să obținem: 1 - minimul

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 - rezultat final

Exersarea întrebărilor de interviu de programare în Python

reduce() cu expresii lambda

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

Scopul este să obținem: 1 - minimul

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

Scopul este să obținem: 1 - minimul

reduce(lambda x, y: x if x < y else y, nums)
1
Exersarea întrebărilor de interviu de programare în Python

Lass uns üben!

Exersarea întrebărilor de interviu de programare în Python

Preparing Video For Download...