Introduction to Python built-in functions in filtermapreduce

Source: Internet
Author: User
Python has built-in interesting and useful functions, such as filter, map, and reduce, which are used to process a set. filter is easy to understand for filtering and map for ING, reduce is used for merging. it is a trigger of the Python list method. Python has some very interesting and useful functions, such as filter, map, and reduce, which are used to process a set, filters are easy to understand for filtering, map for ing, and reduce for merging. it is the trigger of the Python list method.


1. filter functionThe function is equivalent to a filter. Call a Boolean function bool_func to iterate over the elements in each seq. return a sequence of elements that make bool_seq return true.

>>> N=range(10)>>> print filter(lambda x:x>5,N)[6, 7, 8, 9]

2. map functionsFunc acts on each element of a given sequence and uses a list to provide the return value.

>>> N1=[1,2,3]>>> N2=[6,5,4]>>> map(lambda x,y:x+y,N1,N2)[7, 7, 7]>>> map(lambda x:x+3,N1)[4, 5, 6]

3. reduce function, Func is a binary function that acts on the elements of the seq sequence, each carrying a pair (previous results and the elements of the next sequence ), continuously apply the existing results and the next value to the subsequent results, and finally reduce our sequence into a single return value.

>>> N=range(1,101)>>> reduce(lambda x,y:x+y,N)5050

Example 1: Use map and reduce to add 5 factorial (5! + 4! + 3! + 2! + 1 !)

>>>print reduce(lambda x,y:x*y,range(1,6))>>>print reduce(lambda x,y:x*y,range(1,5))>>>print reduce(lambda x,y:x*y,range(1,4))>>>print reduce(lambda x,y:x*y,range(1,3))>>>print reduce(lambda x,y:x*y,range(1,2))'''

Result:

12024621'''

# Convert the result from the previous step into a factorial list

>>>print map(lambda a:reduce(lambda x,y:x*y,range(1,a+1)),range(1,6))[1, 2, 6, 24, 120]

# Add the factorial list. The first question is solved.

>>>print reduce(lambda m,n:m+n,map(lambda a:reduce(lambda x,y:x*y,range(1,a+1)),range(1,6)))153

Example 2: Use filter to convert 100 ~ Filter out prime numbers of less than 200
Prime number. In a natural number greater than 1, except for 1 and the integer itself, it cannot be divisible by other natural numbers.

>>>filter(lambda N:len(filter(lambda M:N%M==0,range(2,int(N**0.5)+1)))==0,range(100,201))

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.