Special syntax in Python: filter, map, reduce, lambda introduction _python

Source: Internet
Author: User

filter (function, sequence): performs a function (item) on the item in sequence, and makes an item that executes the result to true list/string/ Tuple (depending on the type of sequence) returns:

Copy Code code as follows:

>>> 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): performs a function (item) in sequence on the item in sequence, see the Execution results form a list return:

Copy Code code as follows:

>>> 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 ']

The map also supports multiple sequence, which requires that the function also supports the corresponding number of parameter inputs:
Copy Code code as follows:

>>> 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): iterates the call function on the item order in sequence, and if there is a starting_value, it can also be invoked as an initial value. For example, you can use to sum the list:

Copy Code code as follows:

>>> 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 Python's support for an interesting syntax that allows you to quickly define a single line of minimal functions, similar to the macros in C, which are called lambda functions that are borrowed from Lisp and can be used in any place where a function is needed:

Copy Code code as follows:

>>> g = Lambda x:x * 2
>>> g (3)
6
>>> (Lambda x:x * 2) (3)
6

We can also combine filter map reduce with lambda, and functions can be written in a single line.
For example:

Copy Code code as follows:

Kmpathes = Filter (lambda Kmpath:kmpath,
Map (Lambda Kmpath:string.strip (Kmpath),
String.Split (L, ': '))

It looks like trouble, but it's as elegant as a language to describe a problem.
Divide all the elements in L by ': ' and draw a list. Do a string strip to each element of this list to form a list. Do a direct return operation on each element of the list (this place can be added to the filter condition limit), and finally get a string of ': ' Split list, each string in the list is made strip, and can filter on the special string.

---------------------------------------------------------------

Lambda expression returns a function object
Example:

Copy Code code as follows:

Func = Lambda x,y:x+y
Func is equivalent to the following function
def func (x,y):
Return X+y

Note that DEF is a statement and a lambda is an expression
In this case, you can only use lambda instead of def.
Copy Code code as follows:

[(Lambda X:x*x) (x) for x in range (1,11)]

The function in Map,reduce,filter can be generated with a lambda expression!

Map (function,sequence)
Pass the values in sequence to function, and return a list containing the results of the function execution.
If the function has two parameters, that is, the map (FUNCTION,SEQUENCE1,SEQUENCE2).

Example:
Ask 1*1,2*2,3*3,4*4

Copy Code code as follows:

Map (Lambda X:x*x,range (1,5))

The return value is [1,4,9,16]

Reduce (function,sequence)

function can receive only 2 of the number of arguments
Pass the first value in the sequence to the function, and then pass the return value of the function and the third argument to the
function, and then returns only one result.

Example:
1 to 10 of the cumulative

Copy Code code as follows:

Reduce (lambda x,y:x+y,range (1,11))

The return value is 55.

Filter (Function,sequence)

The return value of a function can only be true or false
The value in the sequence is passed as a parameter to the function, and if the return value of function (x) is true, X is added to the filter's return value. Generally, the return value of the filter is list, and in special cases where sequence is a string or tuple, the return value is the type of sequence.

Example:
Find the odd number between 1 and 10

Copy Code code as follows:

Filter (lambda X:x%2!=0,range (1,11))

return value
Copy Code code as follows:

[1,3,5,7,9]


If sequence is a string
Copy Code code as follows:

Filter (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.