SoFunction
Updated on 2024-11-18

A detailed explanation of Map, Filter and Reduce functions in Python

1. Introduction

This article focuses on three special functions Map,Filter and Reduce in Python and how to program code using them. Before we start the introduction, let's understand two simple concepts Higher Order Functions and Lambda Functions.

2. Higher-order functions

Passing in functions as arguments, such functions are called higher-order functions, and functional programming refers to this highly abstract programming paradigm.

Examples are given below:

def higher(your_function, some_variable):
return your_function(some_variable)

In essence, the functions map, filter and reduce are higher-order functions whose input parameters are functions and iterable objects (list, temple, etc.) in the following form:

map(your_function, your_list)
filter(your_function, your_list)
reduce(your_function, your_list)

3. Lambda expressions

Lambda functions are similar to regular Python functions, except that we have to name the regular function, but with lambda functions, it's optional.

The specific syntax is as follows:

lambda inputs: output

Next, let's look at a few common functions and their lambda function equivalent implementations, as follows:

def multiply2(x):           # is the same as
    return x * 2
multiply2 = lambda x: x*2   # lambda function

Another example is as follows:

def add(a, b): # is the same as
return a + b
add = lambda a,b: a+b # lambda function

4. Map function

Map is used to iterate over each element of a Cocoa Iteration object to perform the appropriate conversion operation. For example, by converting the​[1,2,3]​Multiply each element of​[1,2,3] ​convert to​[2,4,6]​. For this, we need a lambda function which is used to implement the operation of multiplying a single element by 2 as follows:

before = [1,2,3]
after = list(map(lambda x:x*2, before))
# after is [2,4,6]

In addition, we are looking at the next example, which is mainly used to convert all characters of a string to uppercase form.

Below:

before = ["apple", "orange", "pear"]
after = list(map(lambda x:(), before))
# after is ["APPLE", "ORANGE", "PEAR"]

Note that although the variable x is used in the lambda function above, we can replace it with any variable name we want as long as it is a valid variable name.

5. Filter function

Filters are used in iterables to retain only certain elements that satisfy certain conditions. For example, keep only odd numbers within [1, 2, 3, 4, 5, 6] and get [1, 3, 5]. Here, we need a lambda function that takes a number and returns True if the number is odd and False otherwise.

The main function of Filter is to select elements from the iterable objects according to specific filter conditions. For example, for the list​[1,2,3,4,5,6]​If we want to filter to get an odd number of elements. We can implement a lambda function that is True when the argument is an odd number and False otherwise.

The code is as follows:

before = [1,2,3,4,5,6]
after = list(filter(lambda x:x%2==1, before))
# after is [1,3,5]

In a chestnut, realize that only strings of length greater than or equal to 5 are retained, i.e. the list of​[“apple”,“orange”,“pear”]​convert to​[“apple”,“orange”]​The code for this is as follows. At this point we can implement a lambda function to receive a string, if the length of the string is greater than or equal to 5, then return True, otherwise False. The code is as follows:

before = ["apple", "orange", "pear"]
after = list(filter(lambda x:len(x)>=5, before))
# after is ["apple", "orange"]

6. Reduce function

The function Reduce is primarily used to combine all the elements in an iterable object in some way. Unlike the function​map​cap (a poem)​filter​is different, we need to introduce a separate​ reduce​, as shown below:

from functools import reduce

In addition, the lambda function used in reduce takes two arguments, which are mainly used to tell us how to combine two elements together. For example, let's say we need to combine the list​[1,2,3,4,5]​The number is obtained by multiplying all the elements in​120​The lambda function we need to implement here is to take two numbers and multiply them. The code example is as follows:

from functools import reduce
before = [1,2,3,4,5]
after = reduce(lambda a,b: a*b, before)
# after is 120

As another example, if we need to use the​-​to concatenate strings. This is done by concatenating the list​["apple", "orange", "pear"]​change into​"apple-orange-pear"​. Here, we need to implement a lambda function that takes 2 strings and combines them with a​-​Characters are added together.

The code is implemented as follows:

from functools import reduce
before = ["apple", "orange", "pear"]
after = reduce(lambda a,b: a+"-"+b, before)

7. Summary

This article from the advanced functions and Lambda functions to start, has introduced Map, Filter and Reduce three advanced function usage, and gives the corresponding code examples.

to this article on a detailed explanation of Python Map, Filter and Reduce function of the article is introduced to this, more related Python Map, Filter, Reduce content, please search my previous posts or continue to browse the following related articles I hope you will support me in the future!