Map (), reduce (), filter, and pythonreduce in python

Source: Internet
Author: User

Map (), reduce (), filter, and pythonreduce in python

It is said that it is a function in functional programming (and some tucao py is not suitable for doing this). In my opinion, it is a method of pythonic.

This simplifies our operations. For example, if we want to add 1 to all the numbers in the list, the most basic thing is to write a function:

In [40]: def add_one(i):   ....:     return i+1   ....: In [41]: for i in range(1, 3):   ....:     print add_one(i)   ....:     23

If map is used, it is simpler:

In [42]: map(add_one, range(1, 3))Out[42]: [2, 3]

 

Actually, pythonic is not enough. After all, we forget the lambda anonymous function.

 

In [44]: map(lambda x: x+1, range(1, 3))Out[44]: [2, 3]

 

Reduce is used to perform operations on iteratable objects in sequence, for example, adding or multiplication objects in sequence.

The built-in descriptions are as follows:

Help on built-in function reduce in module __builtin__:reduce(...)    reduce(function, sequence[, initial]) -> value        Apply a function of two arguments cumulatively to the items of a sequence,    from left to right, so as to reduce the sequence to a single value.    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items    of the sequence in the calculation, and serves as a default when the    sequence is empty.(END)

The usage is as follows:

In [18]: reduce(lambda x, y: x*y, [1, 2, 3])Out[18]: 6
In [19]: reduce (lambda x, y: x * y, [1]) Out [19]: 1In [20]: reduce (lambda x, y: x * y, [], 0) # The last one is the initial value Out [20]: 0

  

 

Filters are used to filter iteratable objects in a way similar to the preceding two methods:

Help on built-in function filter in module __builtin__:filter(...)    filter(function or None, sequence) -> list, tuple, or string        Return those items of sequence for which function(item) is true.  If    function is None, return the items that are true.  If sequence is a tuple    or string, return the same type, else return a list.
In [22]: filter(lambda x: x > 5, range(10))Out[22]: [6, 7, 8, 9]

 

 

The basic usage method is roughly the same as above. I may feel that it is of little use at the beginning. In fact, I will feel very comfortable when I get used to it.

For example, if you want to calculate the sum of 1 to 100, there may be only a few lines of code written by the function, but when you use reduce, there is only one line.

You can use this method to calculate 1! + 2! + .... + 100!

 

(The built-in documentation is awesome ~

 

See:

Http://my.oschina.net/zyzzy/blog/115096

Https://eastlakeside.gitbooks.io/interpy-zh/content/Map%20&%20Filter/index.html

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.