Lambda functions and their usage

Lambda function, also known as anonymous functions, anonymous functions is not the name of the function, the function has no name will do?
Of course. Some function if only a temporary use, but it's also very simple business logic, there is no need to have to give it a name is not available.

First look at a simple lambda function

>>> lambda x, y : x+y
<function <lambda> at 0x102bc1c80>

x and y are two parameters of the function, expression after the colon is the return value of the function, you can see at a glance that this is a function of two variables and in seeking, but as a function name is not how to use it?
Let us here to this anonymous functions bound to a name, so we call the anonymous function makes it possible.

>>> add = lambda x, y : x+y
>>> add
<function <lambda> at 0x102bc2140>
>>> add(1,2)
3

It is equivalent to the conventional function

>>> def add2(x, y):
...     return x+y
...
>>> add2
<function add2 at 0x102bc1c80>
>>> add2(1,2)
3

lambda usage scenarios:

# 1 functional programming: 
for example: a list of integers, arranged in the absolute value of required elements of the list in ascending 
>>> List1 = [3,5, -4, -1,0, -2, -6] 
>> > sorted (List1, the lambda X = Key: ABS (X)) 
[0, -1, -2,. 3, -4,. 5, -6] 

ranking function sorted as a function of a receiving support parameter, which as a sorted ordering basis, in accordance with the absolute value of this sort of list elements. 

Of course, I can also be used to achieve a normal function: 
>>> DEF foo (X): 
... return ABS (X) 
... 
>>> the sorted (List1, Key = foo) 
[0, -1, -2 3, -4, 5, -6] 
is just not enough Pythonic code look this way only. 

lambda: This is the Python supports an interesting syntax that allows you to quickly define the minimum function of a single line, can be used anywhere a function: 
>>> the Add = the X-the lambda, the y-: the X-+ the y- 
>>> the Add (5 , 6) 
11 
>>> (the X-lambda, the y-: the y-the X-+) (5, 6) 
11 

# 2.Python the most common filter screening, map a small brush, reduce the merger, lambda expressions can be used to generate! 
For sequence is concerned, there are three functional programming tools:

map (function, sequence): when the value of the parameter in the sequence by one pass function, returns a list containing the execution result of the function. If the function has two parameters, i.e., map (function, sequence1, sequence2) . 
 
Squaring # 1 to 20 
>>> list (map (lambda x: x * x, range (1,21))) # Python2.x using the map (lambda x: x * x , range (1,21)) 
[. 1,. 4,. 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400] 

filter (function, sequence): for sequence the item in order to perform function (item), the results of the implementation of the item to True to form a List / String / Tuple (depending on the type of sequence) returns. 
 
# Seek an even number between 1 to 20 
>>> list (filter (lambda x: x% 2 == 0, range (1,21))) # Python2.x using filter (lambda x: x% 2 == 0 , Range (1,21)) 
[2,. 4,. 6,. 8, 10, 12 is, 14, 16, 18 is, 20 is] 

the reduce (function, sequence): the number of function parameters is received by only 2, first sequence a first value and a second value when the parameter passed to function, then the function return value and a third value when the parameter passed to function, and only returns a result.
 
1 to # 100 of seeking and
>>> from functools import after reduce # Python3.x reduce the need to import module 
>>> reduce (the lambda X, Y: X + Y, Range (1,101)) 
5050 

# 1 to 100 of summing, plus 10,000 
>> > the reduce (the lambda X, Y: X + Y, range (1,101), 10000) 
15050 

#. 3 closures. 
closures: a function is defined in the interior of the functions, so that even when the variable closure departing from the scope of the range function is also still be accessed. 

View of an example of a function of a lambda closure. 
DEF the Add >>> (n-): 
... return lambda X: X + n- 
... 
>>> the Add ADD2 = (. 5) 
>>> ADD2 (15) 
20 is 

where lambda is a function of a closure, the global scope range, add2 (15) can perform a normal value of 20 and returns. It returns the value 20 because of the role add local scope, the variable n in the closure such that it is the global scope may also be accessed.

Lambda function, also known as anonymous functions, anonymous functions is not the name of the function, the function has no name will do?
Of course. Some function if only a temporary use, but it's also very simple business logic, there is no need to have to give it a name is not available.

First look at a simple lambda function

>>> lambda x, y : x+y
<function <lambda> at 0x102bc1c80>

x and y are two parameters of the function, expression after the colon is the return value of the function, you can see at a glance that this is a function of two variables and in seeking, but as a function name is not how to use it?
Let us here to this anonymous functions bound to a name, so we call the anonymous function makes it possible.

>>> add = lambda x, y : x+y
>>> add
<function <lambda> at 0x102bc2140>
>>> add(1,2)
3

It is equivalent to the conventional function

>>> def add2(x, y):
...     return x+y
...
>>> add2
<function add2 at 0x102bc1c80>
>>> add2(1,2)
3

lambda usage scenarios:

# 1 functional programming: 
for example: a list of integers, arranged in the absolute value of required elements of the list in ascending 
>>> List1 = [3,5, -4, -1,0, -2, -6] 
>> > sorted (List1, the lambda X = Key: ABS (X)) 
[0, -1, -2,. 3, -4,. 5, -6] 

ranking function sorted as a function of a receiving support parameter, which as a sorted ordering basis, in accordance with the absolute value of this sort of list elements. 

Of course, I can also be used to achieve a normal function: 
>>> DEF foo (X): 
... return ABS (X) 
... 
>>> the sorted (List1, Key = foo) 
[0, -1, -2 3, -4, 5, -6] 
is just not enough Pythonic code look this way only. 

lambda: This is the Python supports an interesting syntax that allows you to quickly define the minimum function of a single line, can be used anywhere a function: 
>>> the Add = the X-the lambda, the y-: the X-+ the y- 
>>> the Add (5 , 6) 
11 
>>> (the X-lambda, the y-: the y-the X-+) (5, 6) 
11 

# 2.Python the most common filter screening, map a small brush, reduce the merger, lambda expressions can be used to generate! 
For sequence is concerned, there are three functional programming tools:

map (function, sequence): when the value of the parameter in the sequence by one pass function, returns a list containing the execution result of the function. If the function has two parameters, i.e., map (function, sequence1, sequence2) . 
 
Squaring # 1 to 20 
>>> list (map (lambda x: x * x, range (1,21))) # Python2.x using the map (lambda x: x * x , range (1,21)) 
[. 1,. 4,. 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400] 

filter (function, sequence): for sequence the item in order to perform function (item), the results of the implementation of the item to True to form a List / String / Tuple (depending on the type of sequence) returns. 
 
# Seek an even number between 1 to 20 
>>> list (filter (lambda x: x% 2 == 0, range (1,21))) # Python2.x using filter (lambda x: x% 2 == 0 , Range (1,21)) 
[2,. 4,. 6,. 8, 10, 12 is, 14, 16, 18 is, 20 is] 

the reduce (function, sequence): the number of function parameters is received by only 2, first sequence a first value and a second value when the parameter passed to function, then the function return value and a third value when the parameter passed to function, and only returns a result.
 
1 to # 100 of seeking and
>>> from functools import after reduce # Python3.x reduce the need to import module 
>>> reduce (the lambda X, Y: X + Y, Range (1,101)) 
5050 

# 1 to 100 of summing, plus 10,000 
>> > the reduce (the lambda X, Y: X + Y, range (1,101), 10000) 
15050 

#. 3 closures. 
closures: a function is defined in the interior of the functions, so that even when the variable closure departing from the scope of the range function is also still be accessed. 

View of an example of a function of a lambda closure. 
DEF the Add >>> (n-): 
... return lambda X: X + n- 
... 
>>> the Add ADD2 = (. 5) 
>>> ADD2 (15) 
20 is 

where lambda is a function of a closure, the global scope range, add2 (15) can perform a normal value of 20 and returns. It returns the value 20 because of the role add local scope, the variable n in the closure such that it is the global scope may also be accessed.

Guess you like

Origin www.cnblogs.com/pyyolo/p/11756354.html