The sea does not like to be restrained.
Emektar Üye
- Katılım
- 15 Tem 2021
- Mesajlar
- 1,724
- Çözümler
- 86
- Tepki puanı
- 665
- Ödüller
- 10
- Yaş
- 25
- Sosyal
4 HİZMET YILI
map, filter, and reduce are built-in functions in Python that are often used together for data processing tasks.
map applies a function to each element of an iterable (e.g., a list) and returns a new iterable with the results. Here's an example:
In this example, we define a function square that takes a number and returns its square. We then use map to apply this function to each element of the numbers list, creating a new iterable squares with the squared values.
filter returns an iterable with the elements of an iterable that satisfy a condition (specified as a function). Here's an example:
In this example, we define a function is_even that takes a number and returns True if it is even. We then use filter to create a new iterable evens with the even numbers from the numbers list.
reduce applies a function to the elements of an iterable to produce a single value. Here's an example:
In this example, we import the reduce function from the functools module and define a function multiply that takes two numbers and returns their product. We then use reduce to apply this function to the elements of the numbers list, producing a single value that is the product of all the numbers.
Note that reduce requires at least two arguments (the function to apply and the iterable), and can take an optional third argument (the initial value to use in the reduction). For example:
In this example, we provide an initial value of 10 to the reduce function, which is used as the first argument to the multiply function. This means that the final result will be the product of all the numbers in the numbers list multiplied by 10.
map applies a function to each element of an iterable (e.g., a list) and returns a new iterable with the results. Here's an example:
Python:
def square(x):
return x**2
numbers = [1, 2, 3, 4, 5]
squares = map(square, numbers)
print(list(squares)) # Output: [1, 4, 9, 16, 25]
In this example, we define a function square that takes a number and returns its square. We then use map to apply this function to each element of the numbers list, creating a new iterable squares with the squared values.
filter returns an iterable with the elements of an iterable that satisfy a condition (specified as a function). Here's an example:
Python:
def is_even(x):
return x % 2 == 0
numbers = [1, 2, 3, 4, 5]
evens = filter(is_even, numbers)
print(list(evens)) # Output: [2, 4]
In this example, we define a function is_even that takes a number and returns True if it is even. We then use filter to create a new iterable evens with the even numbers from the numbers list.
reduce applies a function to the elements of an iterable to produce a single value. Here's an example:
Python:
from functools import reduce
def multiply(x, y):
return x * y
numbers = [1, 2, 3, 4, 5]
product = reduce(multiply, numbers)
print(product) # Output: 120
In this example, we import the reduce function from the functools module and define a function multiply that takes two numbers and returns their product. We then use reduce to apply this function to the elements of the numbers list, producing a single value that is the product of all the numbers.
Note that reduce requires at least two arguments (the function to apply and the iterable), and can take an optional third argument (the initial value to use in the reduction). For example:
Python:
product = reduce(multiply, numbers, 10)
print(product) # Output: 1200
In this example, we provide an initial value of 10 to the reduce function, which is used as the first argument to the multiply function. This means that the final result will be the product of all the numbers in the numbers list multiplied by 10.