Python Special Syntax: filter, map, reduce, lambda

Source: Internet
Author: User

filter (function, sequence): Executes function (item) on item in sequence, and the item that executes the result of true is composed of a list/string/ The Tuple (depending on the type of sequence) returns:
>>> 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) : Executes function (item) on item in sequence, and the result of execution consists of a list return:
>>> def Cube (x): Return x*x*x
>>> Map (cube, range (1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> def Cube (x): return x + x
...
>>> map (Cube, "ABCDE")
[' AA ', ' BB ', ' cc ', ' dd ', ' EE ']
In addition, the map 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]


reduce (function, sequence, starting_value) : Calls the function for the item order in sequence, if there is starting_ Value, which can also be called as an initial value, for example, can be used to sum the list:
>>> def add (x, y): return x + y 
>>> Reduce (add, range (1, )  
55 (note: 1+2+3+4+5+6+7+8+9+10)
>>> Reduce (add, range (1, one),)  
75 (Note: 1+2+3+4+5+6+7 +8+9+10+20)


Lambda: This is a funny syntax for Python, which allows you to quickly define the smallest function of a single line, similar to a macro in C, these functions, called Lambda, are borrowed from Lisp and can be used wherever functions are needed:
>>> g = Lambda x:x * 2
>>> g (3)
6
>>> (Lambda x:x * 2) (3)
6


We can also use the filter map reduce and lambda together, the function can be simply written in one line.
For example
Kmpathes = Filter (lambda Kmpath:kmpath,
Map (Lambda Kmpath:string.strip (Kmpath),
String.Split (L, ': ')))
Looks like trouble, in fact, as in the language to describe the problem, very elegant.
Make a list of all the elements in L that are split with ': '. Each element of this list is made into a string strip, forming a list. Do a direct return operation on each element of this list (this place can be added to the filter limit) and eventually get a string that is ': ' In the split list, each string in the list is strip and can be filtered on a special string.

Python Special Syntax: filter, map, reduce, lambda

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.