Special Python Syntax: filter, map, reduce, lambda

Source: Internet
Author: User

Python has built-in interesting but useful functions that fully reflect the charm of Python! Filter (function, sequence): Execute function (item) in sequence for items in sequence, and combine items whose execution result is True into a List/String/Tuple (depending on the sequence type) return: >>> def f (x): return x % 2! = 0 and x % 3! = 0 >>> filter (f, range (2, 25) [5, 7, 11, 13, 17, 19, 23] >>> def f (x ): return x! = 'A'> filter (f, "abcdef") 'bcdef 'map (function, sequence): Execute function (item) in sequence for items in sequence ), see the execution result to form a List and return: >>> def cube (x): return x * x >>>> map (cube, range (1, 11) [1, 8, 27, 64,125,216,343,512,729,100 0] >>> def cube (x): return x + x... >>> map (cube, "abcde") ['A', 'bb ', 'cc', 'dd', 'ee'] In addition, map supports multiple sequence, this requires the function to support the corresponding number of parameter inputs: >>> def add (x, y): return x + y >>>> map (add, range (8 ), Range (8) [0, 2, 4, 6, 8, 10, 12, 14] reduce (function, sequence, starting_value): calls the function sequentially for the items in sequence, if starting_value exists, it can also be called as an initial value. For example, it can be used to sum the List: >>> def add (x, y): return x + y >>> reduce (add, range (1, 11) 55 (Note: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) >>> reduce (add, range (1, 11), 20) 75 (Note: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 20) lambda: this is an interesting syntax supported by Python. It allows you to quickly define the smallest function of a single row. Similar to Macros in C, these lambda functions are borrowed from LISP, it can be used in any function:>> G = lambda x: x * 2> g (3) 6> (lambda x: x * 2) (3) 6. We can also combine filter map reduce and lambda to write a simple row of functions. For example, kmpathes = filter (lambda kmpath: kmpath, map (lambda kmpath: string. strip (kmpath), string. split (l, ':') Looks troublesome. In fact, it is as elegant as using a language to describe the problem. Splits all elements in l by ':' To produce a list. Strip each element of the List to form a list. Perform a direct return operation on each element in the list (filter conditions can be added here) to obtain a list separated, each string in the list is strip and can filter special strings. --------------------------------------------------------------- The lambda expression returns a function object example: func = lambda x, y: x + yfunc is equivalent to the following function def func (x, y ): return x + y note that def is a statement while lambda is an expression. In this case, you can only use lambda instead of def [(lambda x: x * x) (x) for x in range ()] functions in map, reduce, and filter can all be generated using lambda expressions! Map (function, sequence) transmits values in sequence to the function one by one and returns a list containing the function execution result. If a function has two parameters: map (function, sequence1, sequence2 ). Example: 1*3, 4 * 4map (lambda x: x * x, range () Returns [, 9, 16] reduce (function, sequence) the number of parameters received by a function can only be 2. First, pass the first value and the second value in the sequence to the function, and then pass the return value and the third value of the function to the function, then, only one result is returned. Example: Calculate the sum reduce (lambda x, y: x + y, range () of 1 to 10. the return value is 55. The Return Value of the filter (function, sequence) function can only be True or False. Values in the sequence can be passed to the function one by one. If the return value of function (x) is True, add x to the return value of the filter. In general, the return value of filter is list. In special cases, if sequence is string or tuple, the return value is of the sequence type. Example: Find the odd filter (lambda x: x % 2!) between 1 and 10! = 0, range () returns the value [, 9] If sequence is a stringfilter (lambda x: len (x )! = 0, 'Hello') returns 'hello' filter (lambda x: len (x) = 0, 'Hello') returns''

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.