The map function in Python, the filter function, the Reduce function

Source: Internet
Author: User

  Self-learning python, many places need to be a bad supplement. The     three functions are similar, and are all built-in functions that are applied to a sequence. Common sequences include list, tuple, and Str. The  1.map function map function maps the specified sequence according to the provided function. Definition of map function: Map (function, sequence[, sequence, ...]) list by definition you can see that 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 of a sequence: map (Lambda x:x * * 2, [1, 2, 3, 4, 5]) returns the result: [1, 4, 9, 16, 25] when there are multiple sequences in a parameter, the function is called in turn with the element in the same position in each sequence Function. 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,]) The first element in the list returned by the map is the first element of the parameter sequence 1 plus parameter sequence 2 (1 + 2), the second element in the list is the parameter The second element in the number sequence 1 is added to the second element in the parameter sequence 2 (3 + 4), and so on, and the final result is: [3, 7, 11, 15, 19] Note the number of parameters of the function functions to 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, 10]) The results are: [(1, 2), (3, 4), (5, 6), (7, 8), (9)]   The    2.filter function Filter function performs a filtering operation on the specified sequence. Filter function Definition: filter (function or None, sequence), list, tuple, or stringfunction is a predicate function that takes a parameter that returns a Boolean value of TRUE or false. The filter function pairs the sequenceEach element in the parameter sequence invokes a function, 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 sequence, such as returning all the even numbers in the sequence: Def is_even (x): return x & 1!! = 0 filter (Is_even, [1, 2, 3, 4, 5, 6, 7, 8, 9 and 1 0]) 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. The      3.reduce function, the reduce function, accumulates elements in the parameter sequence. The definition of the reduce function: reduce (function, sequence[, initial]), the valuefunction parameter is a function with two parameters, and reduce sequentially takes an element from the sequence. The function is called 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) Results for (  (((((((((((((() +3) +4) +5) +6) reduce (lambda x, y:x + y, [2, 3, 4, 5, 6]) The result is 20  Note that function functions cannot be none. Http://blog.sina.com.cn/s/blog_45ac0d0a010191rb.html

Map function in Python, filter function, reduce function

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.