Python built-in functions filter, map, reduce

Source: Internet
Author: User
Tags python list

filter, map, and reduce are all processed on a set, filter is easy to understand for filtering, map is used for mapping, and reduce is used for merging. Is the Python list method of three carriages.

1. The function of the filter function corresponds to the filter.

the definition of the filter function:Filter (function or None, sequence), list, tuple, or stringfunctions is a predicate function that takes a parameter that returns a Boolean value of TRUE or false. The filter function calls the function function for each element in the sequence parameter sequence, and the result that is returned contains the element that invokes the result to true. The type of the return value is the same as the type of the parameter sequenceFor example, return all the even numbers in a sequence:def is_even (x):return x & 1! = 0 Filter (Is_even, [1, 2, 3, 4, 5, 6, 7, 8, 9, ten])The returned result is:[1, 3, 5, 7, 9]If the function parameter is none, the return result is the same as the sequence parameter. 2.map functionThe map function maps the specified sequence according to the provided function. definition of the map function:map (function, sequence[, sequence, ...]) ListAs you can see by definition, the first parameter of this function is a function, the remaining argument is one or more sequences, and the return value is a collection. Function can be understood as a one-to-one or many-to-a function, the role of map is to call the function function as each element in the parameter sequence, and return a list containing the return value of each function. For example, to perform a square operation on each element in a sequence:Map (Lambda x:x * * 2, [1, 2, 3, 4, 5])The returned result is:[1, 4, 9, +,]when there are multiple sequences in a parameter, the function function is called in turn with the element in the same position in each sequence. For example, the elements in the two sequence are summed sequentially. map (lambda x, y:x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, ten])The first element in the list returned by map is the first element of the parameter sequence 1 plus parameter sequence 2 (1 + 2) .The second element in the list is the second element in the parameter sequence 1 plus parameter sequence 2 (3 + 4) .and so on, the final return result is:[3, 7, one, and a.]Be aware of the number of arguments to the function functions, and match the number of sets provided in the map. if the collection length is not equal, all collections are intercepted at the minimum length. When the function is none, the operation and zip are similar:map (None, [1, 3, 5, 7, 9], [2, 4, 6, 8, ten])The returned result is:[(1, 2), (3, 4), (5, 6), (7, 8), (9, ten)]  3.reduce functionreduce function, the reduce function accumulates elements in the parameter sequence. definition of the reduce function:reduce (function, sequence[, initial]), valueThe function parameter is a two-parameter, and reduce sequentially takes an element from sequence and invokes function again with the result of the last call to function. The first call to function, if supplied with the initial parameter, calls function with the first element in sequence and initial as a parameter, otherwise the function is called with the first two elements in the sequence sequence. reduce (lambda x, y:x + y, [2, 3, 4, 5, 6], 1)reduce (lambda x, y:x + y, [2, 3, 4, 5, 6])The result is Note that function functions cannot be none.  Example:Print Range (6) # 0,1,2,3,4,5
Print Range (1,6) # 1,2,3,4,5
Print reduce (lambda x,y:x*y, Range (1,6)) #1 *2*3*4*5

def action (x):
Return Lambda Y:x+y

F = Action (2) # This is a lambda object
Print F (3) # 5

----------------------------------------------------------------------------------Example 1: Adding the factorial of 5 with map and reduce (5!+4!+3!+2!+1!) >>> print reduce ( lambda x,y:x * y, Code class= "PY Functions" >range ( 1 6 )    
>>> print reduce ( lambda x,y:x * y, Code class= "PY Functions" >range ( 1 5 )    
>>> print reduce ( lambda x,y:x * y, Code class= "PY Functions" >range ( 1 4 >>> print reduce ( lambda x,y:x * y, Code class= "PY Functions" >range ( 1 3 >>> print reduce ( lambda x,y:x * y, range ( 1 , 2 ))    1
Turn The result of 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 ]finally add the factorial list and solve the first problem >>> print reduce ( lambda m,n:m + n, map ( lambda a: reduce ( lambda x,y:x * y, range ( 1 ,a + 1 )), range ( 1 , 6 ))) 153Example 2: Filtering the prime number within 100~200 by using the filter
Prime numbers are also called primes. In a natural number greater than 1, a number that cannot be divisible by other natural numbers except 1 and the integer itself >>> filter ( lambda N: len ( filter ( lambda M:N % M = = 0 , range ( 2 , int (N * * 0.5 ) + 1 ))) = = 0 , range ( 100 , 201 ))  

Python built-in functions filter, map, reduce

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.