O que são as funções map(), filter(), reduce()?

Praticando questões de entrevista de código em Python

Kirill Smirnov

Data Science Consultant, Altran

map()

map(                                                )
Praticando questões de entrevista de código em Python

map()

map(                       Iterable1, Iterable2, ...)

Iteráveis: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...

Praticando questões de entrevista de código em Python

map()

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

Iteráveis: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...

1, 10, ... $\rightarrow$ function(1, 10, ...) $\rightarrow$ novo objeto

2, 20, ... $\rightarrow$ function(2, 20, ...) $\rightarrow$ novo objeto

3, 30, ... $\rightarrow$ function(3, 30, ...) $\rightarrow$ novo objeto

4, 40, ... $\rightarrow$ function(4, 40, ...) $\rightarrow$ novo objeto

5, 50, ... $\rightarrow$ function(5, 50, ...) $\rightarrow$ novo objeto

Praticando questões de entrevista de código em Python

map() com um único Iterable

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

A tarefa é obter [1, 4, 9, 16, 25]

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

squares é iterável

for square in squares:
    print(square)
1
4
9
16
25
Praticando questões de entrevista de código em Python

map() com um único Iterable

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

A tarefa é obter [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]
Praticando questões de entrevista de código em Python

map() com um único Iterable

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

A tarefa é obter [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
Praticando questões de entrevista de código em Python

map() com expressões lambda

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

A tarefa é obter [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]

A tarefa é obter [1, 4, 9, 16, 25]

squares = map(lambda x: x**2, nums)
list(squares)
[1, 4, 9, 16, 25]
Praticando questões de entrevista de código em Python

map() com múltiplos Iterables

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

A tarefa é obter: [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]
Praticando questões de entrevista de código em Python

filter()

filter(                     )
Praticando questões de entrevista de código em Python

filter()

filter(             Iterable)

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

Praticando questões de entrevista de código em Python

filter()

filter(function(x), Iterable)

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

1 $\rightarrow$ function(1) $\rightarrow$ True $\rightarrow$ 1 é mantido

2 $\rightarrow$ function(2) $\rightarrow$ False $\rightarrow$ 2 é rejeitado

3 $\rightarrow$ function(3) $\rightarrow$ True $\rightarrow$ 3 é mantido

4 $\rightarrow$ function(4) $\rightarrow$ False $\rightarrow$ 4 é rejeitado

5 $\rightarrow$ function(5) $\rightarrow$ True $\rightarrow$ 5 é mantido

Praticando questões de entrevista de código em Python

Exemplo de filter()

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

A tarefa é obter: [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
Praticando questões de entrevista de código em Python

Exemplo de filter()

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

A tarefa é obter: [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]
Praticando questões de entrevista de código em Python

Exemplo de filter()

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

A tarefa é obter: [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
Praticando questões de entrevista de código em Python

filter() com expressões lambda

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

A tarefa é obter: [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]

A tarefa é obter: [1, 2, 3]

fobj = filter(lambda x: x > 0, nums)
list(fobj)
[1, 2, 3]
Praticando questões de entrevista de código em Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ novo objeto do mesmo tipo do conteúdo

A representação da lista dada

Praticando questões de entrevista de código em Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ novo objeto do mesmo tipo do conteúdo

Combinando os dois primeiros elementos da lista

Praticando questões de entrevista de código em Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ novo objeto do mesmo tipo do conteúdo

Combinando a saída com o terceiro elemento da lista

Praticando questões de entrevista de código em Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ novo objeto do mesmo tipo do conteúdo

Combinando a saída com o quarto elemento da lista

Praticando questões de entrevista de código em Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

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

[1, 2, 3, 4, 5] $\rightarrow$ novo objeto do mesmo tipo do conteúdo

Combinando a saída com o quinto elemento da lista

Praticando questões de entrevista de código em Python

Exemplo de reduce()

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

A tarefa é obter: 1 - mínimo

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

Praticando questões de entrevista de código em Python

reduce() com expressões lambda

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

A tarefa é obter: 1 - mínimo

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

A tarefa é obter: 1 - mínimo

reduce(lambda x, y: x if x < y else y, nums)
1
Praticando questões de entrevista de código em Python

Vamos praticar!

Praticando questões de entrevista de código em Python

Preparing Video For Download...