Python Learning: Mapping functions (map) and functional programming tools (filter and reduce)

Source: Internet
Author: User

Map functions in a sequence map

The map function applies the passed-in function to each element in a sequence object, and returns a list containing the results of all function calls.

Example 1:

def sum (x): return x + 10l1 = [1,2,3,4,5,6,7]l = Map (sum, L1) #结果为 [11, 12, 13, 14, 15, 16, 17]

Map also has a more advanced use method, such as providing a sequence as a parameter, which is able to return in parallel a list of the results from each of the elements in each sequence as the corresponding parameters of the function. As shown in Example 2.

Example 2:

def sum (x, y): return x + yL1 = [1,2,3,4,5,6]L2 = [10,11,12,13,14,15]print map (sum, L1, L2) #结果为 [11, 13, 15, 17, 19, 21]

Note: Map calls are similar to list parsing, but map applies a function call to each element instead of an arbitrary expression. Because this is idle, in a sense, it becomes a less generic tool, however, in some cases, the map now runs faster than list parsing. It is also faster than the For loop.


Functional Programming tools (filter and reduce)

The map function is the simplest of the built-in functions used by Python for functional programming: Functional programming means a tool that applies some function to the corresponding sequence. For example, some elements (filter) are filtered based on one side of the function, and functions are applied to each pair of elements and run to the final result (reduce). Since both range and filter return an iterative object, in Python3.0 they need a list call to display all of their results.

The elements in the sequence will be typed into the list of results if their return value is true. Like map, this function is equivalent to a for loop, but it is built-in and runs faster.

L = Range ( -10,10) print filter (lambda x:x>4, L) #结果为 [5, 6, 7, 8, 9]

The elements in the sequence will be typed into the list of results if their return value is true. Like map, this function is equivalent to a for loop, but it is built-in and runs faster.

res = []for x in Range ( -10,10): if x >5:res.append (x) print (res) #结果为 [5, 6, 7, 8, 9]


Reduce is a simple built-in function in Python2.6, but it is more complex in Python3.0 in the Functools module.

>>>reduce (lambda x,y:x+y), [1,2,3,4,5]) 15>>>reduce ((Lambda x,y:x*y), [1,2,3,4,5]) 120


Python Learning: Mapping functions (map) and functional programming tools (filter and reduce)

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.