I believe everyone knows what anonymous means. Anonymous means having no name. Sometimes we don’t need to name the function when writing a program. At this time, we can use anonymous functions. We use lambda expressions to use anonymous functions in Python.
Let's use an example to introduce the simple use of the lambda function . The variable m is the value we input. We need to use an anonymous function to return the sum of the squares of m. That is, if we input 2, the return value is 4.
The code is as follows:
m=int(input('Please enter a number:'))#m is the input value a=lambdax:x*x#Use variable a to form an expression print('The return value is:',a(m))
The output is:
Please enter a number: 6. The return value is: 36
Let’s take a look at its structure through the figure below:
Let's use an anonymous function through an example:
m=int(input('Please enter a number:'))#m is the input value a=lambdax:x+10*10+x*xprint('The return value is:',a(m))
The output is:
Please enter a number: 5. The return value is: 130
From the above two examples, we can understand that lambda expression is equivalent to compressing the function into one line of code, and then calling the function directly through the definition of the variable. This method can simplify our code.
We can also use anonymous functions in sequences. Using anonymous functions can help us filter data quickly. See the following example:
It is known that a list is [1,4,6,9,12,23,25,28,36,38,41,56,63,77,88,99], we need to return the even numbers in it and store them in the list among.
We can use the filter function to filter.
The code is as follows:
my_list=[1,4,6,9,12,23,25,28,36,38,41,56,63,77,88,99]print(list(filter(lambdax:x%2==0, my_list)))
The output is:
[4,6,12,28,36,38,56,88]
We analyze this expression from the inside to the outside. The former object in the filter() function is our filtering method, and the latter is the object we want to filter. Then we store these data in the list using the list() function. , and finally print it out. This method can help us integrate data quickly.
We can also formulate rules through anonymous functions when sorting.
First, we know that a set of lists is [('Tuple A',15,33), ('Tuple B',25,26), ('Tuple C',7,7)]. Each The tuple in the element contains the name of each tuple and the minimum and maximum values. We need to sort the list according to the difference between the maximum and minimum values of each person's tuple. See the following code:
my_list=[('Tuple A',15,33),('Tuple B',25,26),('Tuple C',7,7)]my_list.sort(key=lambdax:x[2 ]-x[1])#Use the key keyword to introduce the sorting method. The sorting method is based on the difference between the third element and the second element. The corresponding indexes are 2 and 1print(my_list)
The output structure is:
[('Tuple C',7,7),('Tuple B',25,26),('Tuple A',15,33)]
We can do a simple calculation first. Their differences are 18, 1, and 0 respectively, so their order should be C, B, and A. The return result x[2]-x[1] in the lambda expression , we obtain their difference, and then sort according to the difference.
Anonymous functions are particularly important when filtering data. They can quickly help us solve complex and cumbersome data problems. At the same time, they can optimize our code and make the overall code more concise. We will stop here in this chapter. In the next section, we will learn the three basic functions in functions.