Função filtro () do Python
Exemplo
Filtre a matriz e retorne uma nova matriz apenas com os valores iguais ou superiores a 18:
ages = [5, 12, 17, 18, 24, 32]
def myFunc(x):
if x < 18:
return False
else:
return True
adults
= filter(myFunc, ages)
for x in adults:
print(x)
Definição e uso
A filter()
função retorna um iterador onde os itens são filtrados por meio de uma função para testar se o item é aceito ou não.
Sintaxe
filter(function, iterable)
Valores de parâmetro
Parameter | Description |
---|---|
function | A Function to be run for each item in the iterable |
iterable | The iterable to be filtered |