Advanced functions in Python map/reduce use instances _python

Source: Internet
Author: User
Tags in python

The map () and reduce () functions are built in Python.

If you've read Google's famous paper, "Mapreduce:simplified Data processing on Large clusters," you can probably understand the concept of map/reduce.

Let's look at the map first. The map () function receives two parameters, one is a function, the other is a sequence, and map functions the passed-in function to each element of the sequence and returns the result as a new list.

For example, for example, we have a function f (x) =x2, to use this function in a list [1, 2, 3, 4, 5, 6, 7, 8, 9], you can do this with a map ():

Now we're going to use Python code:

Copy Code code as follows:

>>> def f (x):
... return x * x
...
>>> map (f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]

The first parameter passed by map () is F, which is the function object itself.

You might think that you don't need a map () function, write a loop, or you can calculate the result:

Copy Code code as follows:

L = []
For n in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
L.append (f (n))
Print L

Yes, but from the loop code above, can you see "f (X") acting on every element of the list and generating a new list of the results?

So, as a higher order function, map () actually abstracts the rules of operation, so we can compute not only the simple f (x) =x2, but also any complex function, for example, to convert all numbers of this list into strings:

Copy Code code as follows:

>>> map (str, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ']

Only one line of code is required.

See the use of reduce again. Reduce functions a function in a sequence [X1, x2, x3 ...] , this function must receive two parameters, reduce to continue the result and the next element of the sequence cumulative calculation, the effect is:

Copy Code code as follows:

Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)

For example, to sum a sequence, you can use reduce to implement:
Copy Code code as follows:

>>> def add (x, y):
... return x + y
...
>>> reduce (add, [1, 3, 5, 7, 9])
25

Of course the sum can be used directly with the Python built-in function sum (), no need to use reduce.

But converting a sequence [1, 3, 5, 7, 9] into an integer 13579,reduce can be useful:

Copy Code code as follows:

>>> def fn (x, y):
... return x * + y
...
>>> reduce (FN, [1, 3, 5, 7, 9])
13579

The example itself is of little use, but if you take into account that string Str is also a sequence, with a slight change to the example above, with map (), we can write a function that converts str to int:
Copy Code code as follows:

>>> def fn (x, y):
... return x * + y
...
>>> def char2num (s):
... return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[s]
...
>>> reduce (FN, map (char2num, ' 13579 '))
13579

The function of organizing into a str2int is:
Copy Code code as follows:

def str2int (s):
DEF fn (x, y):
return x * + y
def char2num (s):
return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[s]
Return reduce (FN, map (Char2num, s))

You can also use a lambda function to further simplify:
Copy Code code as follows:

def char2num (s):
return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[s]

def str2int (s):
return reduce (lambda x,y:x*10+y, map (Char2num, s))


In other words, assuming that Python does not provide an int () function, you can write a function that converts a string to an integer, and it only takes a few lines of code!

Related Article

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.