This article allows you to thoroughly grasp [lambda functions in Python]

You should have heard that the application of Python allows you to reduce the repetitive workload of a day to a few minutes or even less. Since then, free working hours and study more and more efficient working methods. Further improve work efficiency and make work more brilliant. This is not an advertisement, this is a real hammer picture.

This article will explore the lambda function in Python with you, so that you can understand the principle of this function in the shortest time. You can also use the fragmented time to consolidate this function to make you more efficient in the process of processing.
  
insert image description here
  
  

1. Definition of lambda function

    
The lambda function is a commonly used built-in function in Python, also known as an anonymous function. Compared with ordinary functions, it only has a function body, and def and return are omitted, making the structure look more streamlined. Its basic call syntax is as follows:

lambda [var1 [,var2,…varn]]:expression

[var1 [,var2,…varn]]: Formal parameters, which can be understood as input parameters for use in expressions.

expression: Function expression whose result is the return value of the lambda function.

  
  

Two, lambda function instance

  

Example 1: Comparison of lambda functions and ordinary functions

  
Define a lambda function, assign it to a variable, and call the function indirectly through this variable.

fun_1 = lambda a,b: a*2+b*3
fun_1(1,2)

got the answer:

8

Among them, a and b are formal parameters, that is, input parameters, which are used by the expression a 2+b 3.
  
Define an ordinary function, the statement is as follows:

def fun1(a,b):
    return a*2+b*3
fun1(1, 2)

got the answer:

8

It can be found that the difference between the application of the lambda function and the ordinary function is that the def and return statements are missing, and the lambda function is directly written on one line, which is more streamlined. However, the anonymous attribute has not yet been reflected here.

  

Example 2: Find a quadratic equation in one variable

  
When we were in junior high school, we often saw such a question: F(x)=a x^2+b x+c, when a=2, b=3, c=4, F(-1) , F(2), F(5) values.
  
Method 1: Apply the lambda function to solve

F_x = lambda x:2*x**2+3*x+4
print(F_x(-1))
print(F_x(2))
print(F_x(5))

got the answer:

3
18
69

  
Method 2: Apply ordinary functions to solve

def F_x(x):
    return 2*x**2+3*x+4
print(F_x(-1))
print(F_x(2))
print(F_x(5))

got the answer:

3
18
69

It can be found that the solution results of applying lambda function and ordinary function are the same.

  

Example 3: Used in conjunction with the higher-order function filter

  
The high-order function filter will be described in detail in subsequent articles. It is mainly used to filter the list and select values ​​in the list that meet specific conditions. For example, if you want to select multiples of 3 in the sequence, the statement is as follows:

cs_data = list([2, 8, 11, 12, 15, 6, 1, 7])
list(filter(lambda x:x%3==0, cs_data))

got the answer:

[12, 15, 6]

It can also be seen with common sense that the final result is to select a multiple of 3 in the sequence cs_data. When used in conjunction with higher-order functions, the advantages of lambda functions are further highlighted. At this time, there is no need to define an ordinary function in advance, and the result of the lambda function can be directly passed to the higher-order function as a parameter inside the higher-order function, and it is truly anonymous.

  

Example 4: Use with the sorted function

  
If we want to arrange a series of numbers according to the absolute value of the value, we can use the sorted function and the lambda function to achieve it. The statement is as follows:

my_list = [3, 5, -2, 10, -6, 8]
sorted(my_list, key=lambda x:abs(x))

got the answer:

[-2, 3, 5, -6, 8, 10]

It can be found that the obtained result is a sequence sorted by the absolute value of the value.

So far, the lambda function in Python has been explained. If you want to know more about functions in Python, you can go to the official account of "Ali Yiyang's Code" to read related articles about the "Learning Python" module.

  
You may be interested in:
Draw Pikachu with Python
Draw a word cloud map
with Python Draw 520 eternal heart beats with Python Python face recognition - you are the only one
in my eyes With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Long press (scan) to recognize the QR code above to learn more Python and modeling knowledge, making your study and work more brilliant.

Guess you like

Origin blog.csdn.net/qq_32532663/article/details/125247034