In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the usage of Python anonymous function lambda". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of the Python anonymous function lambda.
Python has several built-in keywords, and today we'll introduce the lambda keyword that is very useful, which can be used to represent a function in an expression.
Syntax format of lambda expression: lambda [parameter_list]: expression
You can see several key points of lambda expressions from the syntax format:
Lambda expressions must be defined using the lambda keyword
Lambda expressions can only be single-line expressions
Parameter_list is a parameter list
Expression is an expression
Lambda expressions are essentially anonymous, one-line function functions, so lambda expressions are also called lambda functions. As a function, its input is the value passed into the parameter list argument_list, and the output is the value calculated according to the expression expression.
Here are some examples of lambda functions:
The lambda x args:sum function input is x and y, and the output is their product x*ylambda: None # function has no input parameters, the output is the Nonelambda * args:sum (args) # function input to any number of parameters, the output is their sum (implicitly requires that the input parameters must be able to add) lambda * * kwargs:5 # input as any key-value pair parameter, output is 5
Lambda expressions can also be written as functions. For example, the above lambda expression lambda x _ line _ y can be rewritten as a function:
Def product (xrecoy): return xperiy
The product () function has only one line of code, and the use of lambda expressions can save the process of def defining the function and play the role of a function sketch. In general, however, functions are more adaptable than lambda expressions and can be used to handle more powerful tasks. The body of an lambda expression is an expression, not a block of code, and can only encapsulate limited logic in an lambda expression.
The lambda syntax is fixed, and in essence only one lambda function can be defined. According to the defined lambda function, it can be extended to the following uses:
The lambda syntax is fixed, and in essence only one lambda function can be defined. According to the defined lambda function, it can be extended to the following uses:
1. Assign the lambda function to a variable through which the lambda function is called indirectly. For example, the lambda function lambda x _ product _ y defines the multiplication function and assigns it to the variable product, so that the variable product becomes a function with multiplication function, as shown below:
Product = lambda xjingyizhi xjuyprint (product (2jin5)) # 10
2. Pass the lambda function as an argument to other functions. Some of the Python built-in functions can take functions as arguments. Typical examples are filter (), map (), sorted (), reduce (), and so on. The general principle is similar. Here we introduce calling lambda expressions with the built-in function map ().
The first argument to the map function needs to be passed in, and here the lambda function is used to specify a common operation on each element in the list, which makes the program more concise and performs better. As follows:
# the lambda expression for multiplication is passed in as the parameter result = map (lambda xpeny: Xyogy, range (8), range (8)) print ([e for e in result]) # [0,1,4,9,16,25,36,49] result = map (lambda xmaginy: xonomy if x% 2 = = 0 else 0, range (8), range (8)) print ([e for e in result]) # [0,0,4,0,16,0,36,36 0] so far I believe you have a deeper understanding of "the use of Python anonymous function lambda", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.