Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python mapping and filtering and reducing functions

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to use Python mapping and filtering and reduction functions, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Python provides a number of predefined built-in functions that end users can use by calling them. These features not only simplify the work of programmers, but also create a standard coding environment.

You'll learn about three impressive functions in Python, namely map (), filter, and reduce ().

Before we continue, let's take a look at the content:

What are the map,filter and reduce functions in Python?

Use user-defined functions and lambda functions in the following ranges:

Map () function

Filter () function

Reduce () function

Use the map (), filter () and reduce () functions together

Filter () in map ()

Map () in filter ()

Map () and filter () with reduce ()

So let's get started. :)

What are the map (), filter (), and reduce () functions in Python?

As mentioned earlier, map (), filter (), and reduce () are built-in functions for Python. These features enable the functional programming aspects of Python. In functional programming, the parameters passed are the only factors that determine the output. These features can use any other function as a parameter, or they can be provided to other functions as a parameter. Now let's take a closer look at these features.

Map () function:

The map () function is a high-order function. As mentioned earlier, this function takes another function as an argument along with an iterable sequence and returns output after applying the function to each iterable sequence that exists in the sequence. The syntax is as follows:

Syntax:

Map (function, iterable)

Here, the function defines an expression, which in turn applies to iterable objects. The map function can take user-defined functions as well as lambda functions as arguments.

Use user-defined functions and Lambda functions in the following scope: user-defined functions in map ():

The map () function can take a user-defined function as an argument. The parameters for these functions are specifically set by the user or programmer. For example:

Example:

Def newfunc (a): return a*ax = map (newfunc, (1 x is the map objectprint (x) print (set (x))

Output:

{16, 1, 4, 9}

As you can see, x is a map object. The next part of the output shows the map function that takes newfunc () as an argument, and then applies a * a to all iterable objects. As a result, the values of all iterable variables multiply themselves and return.

Note: the output is not in the order of iteratable values, because I have used the set () function. You can also use the list () or tuple () functions, for example:

Example:

Def newfunc (a): return a*ax = map (newfunc, (1 x is the map objectprint (x) print (list (x))

Output:

[1, 4, 9, 16]

You can also pass multiple parameter lists. For example:

Example:

Def func (a, b): return a + b a = map (func, [2,4,5], [1meme 2jue 3]) print (a) print (tuple (a))

Output:

(3, 6, 8)

Now let's see how to use the lambda function in the map () function.

The Lambda function in map ():

The Lambda function is a function with any name. These functions are usually provided as parameters to other functions. Now let's try to embed the lambda function in the map () function. Consider the following example:

Example:

Tup= (5, 7, 22, 97, 54, 62, 77, 23, 73, 61) newtuple = tuple (map (lambda x: Xerox, tup)) print (newtuple)

Output:

(8, 10, 25, 100, 57, 65, 80, 26, 76, 64)

The above output is the result of applying the lambda expression (x + 3) to each item that exists in the tuple.

Filter () function:

The filter () function is used to create an output list of values for which the value returns true. Its syntax is as follows:

Syntax:

Filter (function, iterable)

Just like map (), you can use this function, or you can use user-defined functions and lambda functions as arguments.

Example:

Def func (x): if x > = 3: return xy = filter (func, (1 print) print (list (y))

Output:

[3, 4]

As you can see, y is a filter object, and the list is a list of values that are correct for the condition (x > = 3).

Use lambda in filter ():

The lambda function used as an argument actually defines the condition to check. For example:

Example:

Y = filter (lambda x: (x > = 3), print (list (y))

Output:

[3,4]

The above code produces the same output as the previous function.

Reduce () function:

As the name implies, the reduce () function applies a given function to an iterable object and returns a single value.

The syntax for this function is as follows:

Syntax:

To reduce (a function, iteratively).

The function here defines which expressions need to be applied to iterable objects. This feature needs to be imported from the functools module. For example:

Example:

From functools import reducereduce (lambda a _ r _ r b: a _ r _ b, [23 _ r _ 21 _ r _ 45 _ r 98])

Output: 187

In the above example, the reduce function continuously adds each iterable object that exists in the list and returns a single output.

The map (), filter (), and reduce () functions in Python can be used together.

Use the map (), filter (), and reduce () functions together:

When you do this, the internal function is parsed first, and then the external function operates on the output of the internal function.

Let's first try to pass the filter () function as an argument to the map () function.

Use filter () in map ():

The code shown below first checks whether the condition (x > = 3) is true for iterable objects. Then, use the map () function to map the output.

Example:

C = map (lambda x lambda x: (x > = 3), (1meme 2m 3e 4)) print (list (c))

Output: [6pc8]

If integers greater than or equal to 3 are filtered from a given tuple, the result is [3p4]. Then, if you map this condition using the (x + x) condition, you will get [6p8], which is the output.

Use map () in filter ():

When you use the map () function in the filter () function, iterable objects are first manipulated by the map function, and then the conditions of filter () are applied to them.

Example:

C = filter (lambda x: (x > = 3), map (lambda x:x+x, (1 lambda 2 and 3) # lambda x: (x > = 3) print (list (c))

Output: [4, 6, 8]

Use map () and filter () in reduce ():

The output of the inner function is reduced according to the conditions provided to the reduce () function.

Example:

D = reduce (lambda x Magi y: X lambda Magi map (lambda x: (x > = 3), (1m 2m 3) print (d)

Output: 14

The output is the result of [6J8], which is the result of the internal map () and filter () functions.

At this point, we have concluded the article on the reduce function in map (), filter (), and Python. I hope you have a clear understanding of everything.

After reading the above, have you mastered how to use Python mapping and filtering and reducing functions? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report