Python self-study note-map and reduce functions (from Liaoche's official website Python3)

Source: Internet
Author: User

feel Liao Xuefeng's official website http://www.liaoxuefeng.com/inside the tutorial is good, so study, the need to review the excerpt. The following mainly for their own review, details please log on to the official website of Liao Xuefeng to view. Python has a built-inthe map () and reduce () functions. Let's look at map first.The map () function receives two parameters, one is a function, the other is iterable, andmap passes the incoming function to each element of the sequence sequentially, returning the result as a new iterator. For example, for example, we have a function f (x) =x2, to function in a list[1, 2, 3, 4, 5, 6, 7, 8, 9], you can use map () to achieve the following: Now, we use Python code to implement:>>> def f (x):... return x * x ...>>> r = map (f, [1, 2, 3, 4, 5, 6, 7, 8, 9])>>> List (R) [1, 4, 9, +, A, $, $, a, Bayi]map () The first parameter passed in is F, which is the function object itself. Since the result R is a Iterator,Iterator is an inert sequence, so the list () function allows it to calculate the entire sequence and return a list. Look againusage of reduce. reduce functions a function in a sequence [x1, x2, x3, ...] , the function must receive two parameters, andreduce calculates the result and the next element of the sequence, and the effect is: reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)For example, to sum a sequence, you can useReduce implementation:>>> from functools import reduce>>> def add (x, y):... return x + y ...>>> Reduce (add, [1, 3, 5, 7, 9])Of course, the sum operation can be directly built into Python functionssum (), no need to use reduce. But if you want to put the sequence[1, 3, 5, 7, 9] transform into an integer 13579,reduce can come in handy: >>> from functools import reduce>>> def fn (x, y):... return x * ten + y ...>>> Reduce (FN, [1, 3, 5, 7, 9])13579This example is not very useful in itself, but if you consider the stringSTR is also a sequence, a slight change to the above example, with map (), we can write the function of converting str to int: >>> from functools import reduce>>> def fn (x, y):... return x * ten + y ...>>> def char2num (s):... return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 8, ' 9 ': 9}[s] ...>>> Reduce (FN, map (char2num, ' 13579 ')) 13579

You can also use lambda functions to further simplify:From functools import reducedef 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 * + y, map (Char2num, s))That is, suppose Python does not provideint () function, you can write a function that converts a string to an integer by itself, and only requires a few lines of code! The use of lambda functions is described later.

Python self-study note-map and reduce functions (from Liaoche's official website Python3)

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.