Python functional Programming--map (), Reduce ()

Source: Internet
Author: User

Article Source: http://www.pythoner.com/46.html

It's not strange to mention map and reduce, Google Inc. introduced a programming model called MapReduce in 2003 [1], which is used to process massive amounts of data and is then widely used in Google's applications. The 2006 Apache Hadoop Project [2] formally included MapReduce in the project.

Well, gossip less, today is about the other two built-in functions in Python's functional programming map() reduce() , and not Google's mapreduce.

1.map ()

Format:map( func, seq1[, seq2...] )

Functions in Python's functional programming map() are to act on each element of the SEQ with Func and give the return value with a list. If Func is None, the function is the same zip() .

when the SEQ has only one , the Func function is applied to each element of this seq and a new seq is obtained. Shows how the map () function works when there is only one seq (the source of this picture: "Core Python Programming (2nd Edition)").

As you can see, each element in the SEQ is acting as a func function, resulting in a list of Func (Seq[n]).

Here is an example to illustrate. Suppose we want to get the remainder of the number%3 in a list, then we can write the following code.

Python-Functional programming map using (a seq)Python
123456 # using Mapprint map( Lambda x: x%3, range(6 ) # [0, 1, 2, 0, 1, 2] #使用列表解析print [x%3 for x in range(6)] # [0, 1, 2, 0, 1, 2]

This is the same as the last time filter() , using the method of list parsing instead of map execution. So, when is list parsing not a substitute for map?

Originally, when the SEQ is more than one , map can execute the process as shown in parallel to each SEQ:

That is, each SEQ element in the same position is given a return value after executing a multivariate Func function, and these return values are placed in a list of results.

The following example is for the product of the corresponding element of the two list, which can be imagined as a condition that is likely to occur frequently, and if it is not a map, use a For loop, and then execute the function for each position in turn.

Python-Functional programming map using (multiple seq)Python
1 Print Map( Lambda X, Y: X * y, [1, 2, 3], [4, 5, 6] ) # [4, ten,]

The above is a case where the return value is a value, and it can actually be a tuple. The following code does not only implement multiplication, it also implements addition, and puts the product and sum in a single tuple.

Python-Functional programming map using (multiple seq)Python
1 Print Map( Lambda X, Y: ( X * Y, X + y) , [1, 2, 3< Span class= "Crayon-sy" >], [4, 5 6]   # [(4, 5), (7), (9)]

And that's what it says. The func is None , and its purpose is to merge elements of multiple lists in the same position into a tuple, which now has a dedicated function zip() .

Python-Functional Programming map use (Func is None)Python
123 Print map ( None, [1 , 2, 3], Span class= "Crayon-sy" >[4, 5, 6 )   # [(1 , 4), (2, 5), (3, 6)] print zip( [1, 2, 3], [4, 5, 6] ) # [(1, 4), (2, 5), (3, 6)]

It is important to note that multiple seq of different lengths cannot execute the map function and there will be a type error.

2.reduce ()

Format:reduce( func, seq[, init] )

The reduce function is a simplification, which is a process in which the last iteration result (the element with the first init, such as the first element of the SEQ without init), executes a two-dollar func function with the next element, each iteration. In the reduce function, init is optional and, if used, is used as the first element of the first iteration.

In simple terms, you can use a visual formula to illustrate:
reduce( func, [1, 2,3] ) = func( func(1, 2), 3)

The following is a diagram of the work process of the reduce function:

For example, factorial is a common mathematical method, and Python does not give a factorial built-in function, and we can use reduce to implement a factorial code.

reduce usage of python functional programmingPython
12 n = 5 Print Reduce(Lambda x, y: x * y, range(1 , n + 1)) #

So, what if we want to get twice times the factorial value? This allows you to use the optional parameters of init.

reduce usage of python functional programmingPython
123 m = 2 n = 5 Print Reduce( Lambda X, y: X * y , range ( 1, n + 1 ) , m )    #

3. References

[1] Lammel R. Google's MapReduce programming model–revisited[j]. Science of computer Programming, 2008,70 (1): 208-237.
[2] hadoop[eb/ol]. Http://hadoop.apache.org.

Python functional Programming--map (), Reduce ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.