filter()
filter(function, iterable)numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # Выведет [2, 4, 6, 8, 10]string = "Hello, World!"
vowels = filter(lambda char: char.lower() in 'aeiou', string)
print(''.join(vowels)) # Выведет "eoo"class Person:
def __init__(self, name, age):
self.name = name
self.age = age
people = [
Person("Alice", 25),
Person("Bob", 30),
Person("Charlie", 35),
Person("David", 40)
]
adults = filter(lambda person: person.age >= 30, people)
for adult in adults:
print(f"{adult.name} ({adult.age})")
# Выведет:
# Bob (30)
# Charlie (35)
# David (40)Последнее обновление