Python built-in function filter map reduce introduction _python

Source: Internet
Author: User
Tags python list

Python has built-in some very interesting and useful functions, such as filter, map, and reduce, which are all processing a set, and filter is easy to understand for filtering, map for mapping, and reduce for merging. Is the Python list method of three carriages.


1. The function of the filter function is equivalent to the filter. Call a Boolean function Bool_func to iterate through the elements in each SEQ; returns a sequence of elements that make bool_seq return a value of true.

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

2. The map function func Each element of a given sequence and provides a return value using a list.

>>> 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. The reduce function , func, is a two-element function that func the elements of a SEQ sequence, each carrying a pair (the previous result and the elements of the next sequence), continuing to effect the existing results and the next value on the resulting subsequent result, Finally, we reduce our sequence to a single return value.

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

Example 1: Using map and reduce to implement the factorial addition of 5 (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 is

6
2
1
""

#把上一步的结果变成一个阶乘列表

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

#最后把阶乘列表相加, solve the first problem.

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

Example 2: Filter the number of prime numbers within the 100~200 with filter
Prime numbers are also called primes. In a natural number greater than 1, except for 1 and the integer itself, the number that 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))
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.