A lambda function is a quick definition of the smallest function of a single line, borrowed from Lisp, and can be used wherever a function is needed. The following example compares the traditional definition of a function with a lambda function.
A few days ago, I saw a line of Python code that asked for 1000 factorial.
Python code
Print reduce (lambda x,y:x*y, range (1, 1001))
All of a sudden by the Python code of Simplification and compact, so the code for a simple analysis.
Reduce and range are Python's built-in functions.
Range (1,1001) represents a list of contiguous integers that generate 1 to 1000.
Reduce (Functiona,iterableb), Functiona to a function that requires two variables, and returns a value. Iterableb is an iterative variable, such as list. The reduce function passes the elements in B to function A from left to right, and then replaces the incoming parameter with the result returned by function A, and then it can be reduce to a single value. Here, you pass the list of consecutive integers from 1 to 1000 into the lambda function and replace the number in the list with a two-number product, and the actual calculation process is: (...) ((1x2) x3) x4) x...x1000), the final result is the factorial of 1000.
Here's a look at the lambda function.
A lambda function is a quick definition of the smallest function of a single line, borrowed from Lisp, and can be used wherever a function is needed. The following example compares the traditional definition of a function with a lambda function:
>>> def f (x,y):
... Return x*y ...
>>> f (2,3)
>>> g = lambda x,y:x*y
>>> g (2,3)
As you can see, two functions get the same result, and for functions that implement simple functions, the lambda function is used to define the more streamlined and flexible, and the function can be assigned directly to a variable, and the name of the function is represented by the variable name.
In fact, lambda functions are often not required to assign values to a variable (such as the process of factorial in the preceding article).
There are some caveats to using the lambda function:
A lambda function can receive any number of arguments, including optional arguments, and returns the value of a single expression.
A lambda function cannot contain a command and cannot contain more than one expression.
Here's a quick example of how to implement a custom sort using a lambda function.
class people:
age=0
gender= ' male '
def __init__ (self, Age, gender):
Self.age = age
Self.gender = Gender
def toString (self): return
' Age: ' +str (self.age) + ' \tgender: ' +self.gender
list=[people (21, ' Male '), people (' Famale '), people (+, ' male '), people ("Famale")]
print ' Befor sort: ' for
p in List:
Print p.tostring ()
List.sort (Lambda p1,p2:cmp (p1.age,p2.age))
print ' \nafter ascending sort: ' for
p in List:
print p.tostring ()
List.sort (Lambda p1,p2:-cmp (p1.age,p2.age))
print ' \nafter descending sort: ' For
p in List:
print p.tostring ()
The code above defines a people class and, through a lambda function, implements a list of the People class objects in ascending and descending order by the age of people. The results of the operation are as follows:
Befor Sort:
Age:21 Gender:male
Age:20 Gender:famale
Age:34 Gender:male
Age:19 Gender:famale
After ascending sort:
Age:19 Gender:famale
Age:20 Gender:famale
Age:21 Gender:male
Age:34 Gender:male
After descending sort:
Age:34 Gender:male
Age:21 Gender:male
Age:20 Gender:famale
Age:19 Gender:famale
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.