[Python] functions Lambda (), filter (), map (), reduce ()

Source: Internet
Author: User
ArticleDirectory
    • 1. Lambda ()
    • 2. Filter ()
    • 3. Map ()
    • 4. Reduce ()
    • 5. Comprehensive examples
1. Lambda ()

Lambda () is an anonymous function in Python. Its syntax is as follows:

Lambda [arg1 [, arg2,... argn]: Expression

The following is an example of 1 + 2 = 3.

 

 
>>> Fun = Lambda X, Y: x + y >>> fun (1, 2) 3
 
>>> (Lambda X, Y: X + Y) (1, 2) 3
 
 

 

 
 
2. Filter ()

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) to 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'

 

3. Map () 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 also supports multiple sequence, which requires that the function also supports 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]
4. Reduce ()

Reduce (function, sequence, starting_value): calls the function sequentially for 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)
5. Comprehensive examples

The following are two examples of using the above four functions:

Example 1: Computing 5! + 4! + 3! + 2! + 1!
 
A = [5, 4, 3, 2, 1] def fun (x): Result = 1 while x> = 1: result = Result * x = x-1 return resultprint reduce (lambda X, Y: X + Y, map (fun, ))

Example 2:Select a prime number of less than 100

 
Import math def isprime (n): If n <= 1: Return false for I in range (2, INT (math. SQRT (n) + 1): If n % I = 0: Return false return True Print filter (isprime, range (1,100 ))
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.