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 analyze the map and reduce functions of python

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

Share

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

This article introduces how to analyze the map and reduce functions of python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

The editor is talking about the map and reduce functions of Python.

This pair of brothers are extremely frequent and quite practical python functions, which will be difficult for beginners to understand. You can fix them after reading this article.

01

Map

The map () method maps a function to each element of the sequence, generating a new sequence that contains the return values of all functions.

That is to say, every element in the sequence is put into a function f (x) as an x variable, and the result is f (x1), f (x2), f (x3). To form a new sequence.

How do I use the map function?

Map (function_to_apply, list_of_inputs)

Function_to_apply: representative function

List_of_inputs: represents the input sequence

Note: the map function in python3 returns an iterator

Most of the time, we pass all the elements in the list to a function one by one and collect the output.

For example:

Items = [1, 2, 3, 4, 5] # list squared = [] for i in items: squared.append (iTunes 2)

The map function allows us to implement it in a much simpler and much more beautiful way:

Items = [1, 2, 3, 4, 5] squared = list (map (lambda x: Xerox, items))

Anonymous functions are used above, or you can customize the functions:

Items = [1, 2, 3, 4, 5] def f (x): return xanthates 2 squared = list (map (f, items))

02

Reduce

Reduce is a little more complicated than map.

The working process of reduce is as follows: in the process of iterative sequence, the first two elements (only two) are passed to the function, and then the result and the third element are passed to the function parameters as two parameters, and the result and the fourth element are passed to the function parameters as two parameters, and so on.

How to use the reduce function?

Reduce (function, iterable [, initializer])

Function: representative function

Iterable: sequence

Initializer: initial value (optional)

Unlike map, reduce cannot be used directly and needs to be imported with from functools import reduce

For example, if I ask for a factorial of 10, I can do it with reduce:

# Import the reduce from functools import reduce # definition function def f (XMagi y): return x quoy # defines the sequence, with the element items = range (1meme 11) # using the reduce method result = reduce (fame items) print (result) on how to analyze the map and reduce functions of python. That's it. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report