In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is the use of Lambda function". In daily operation, I believe many people have doubts about the use of Lambda function. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what is the use of Lambda function?" Next, please follow the editor to study!
1. What is the Lambda function?
Python supports an interesting syntax that allows you to quickly define a single-line minimum function. These functions, called Lambda, are borrowed from Lisp and can be used wherever functions are needed. The Lambda function is an anonymous function that can accept any number of parameters, including optional parameters, and return a single expression value. It is important to note that Lambda functions cannot contain commands, nor can they contain more than one expression.
Example:
> lambda x, y: X roomy
2. The syntax of Lambda function.
In the Lambda statement, the colon is preceded by a parameter, which can be 0 or more, separated by a comma, and the return value to the right of the colon. What the Lambda statement builds is actually a function object.
(1) No parameters:
F = lambda:'Hello python lambda'
F ()
# 'Hello python lambda'
(2) there are parameters but no default values
F = lambda XBI y: XBY
F (6pc7)
# 42
(3) there are parameters and default values
F = lambda Xero5, YBG 8: XBY
F ()
# 40
F (5pr 6)
# 30
(4) used with map, reduce, filter
Foo = [2,18,9,22,17,24,8,12,27]
Print filter (lambda x: X% 3 = = 0, foo) # python 2.x
List (filter (lambda x: X% 3 = = 0, foo)) # python 3.x
# [18, 9, 24, 12, 27]
Print map (lambda x: X * 2 + 10, foo) # python 2.x
List (map (lambda x: X * 2 + 10, foo)) # python 3.x
# [14, 46, 28, 54, 44, 58, 26, 34, 64]
From functools import reduce#python 3.x need import reduce
Reduce (lambda x, y: X + y, foo)
# 139
3. The usage scenario of Lambda function.
(1) functional programming
For example: a list of integers that is required to be sorted in ascending order by the absolute values of the elements in the list
> list1 >
> sorted (list1, key=lambda x: abs (x))
[0, 1,-2, 3,-4, 5,-6]
The sorting function sorted supports receiving a function as a parameter, which is sorted by the absolute value of the list element, which is used as the sorting basis for sorted.
Of course, I can also use ordinary functions to do this:
> > def foo (x):
... Return abs (x)
...
> sorted (list1, key=foo)
[0, 1,-2, 3,-4, 5,-6]
It's just that the code doesn't look Pythonic enough in this way.
> add = lambda XJI y: XBY
> add (5pc6)
eleven
> (lambda XJI YGU Xuey) (5je 6)
eleven
(2) the most common filter filters, map brushes, and reduce merges in Python can all be generated by lambda expressions! For sequences, there are three functional programming tools: filter (), map (), and reduce (). Map (function,sequence): passes the values in sequence as parameters to function one by one, and returns a list containing the result of the function execution. If function has two parameters, map (function,sequence1,sequence2).
# find the square of 1: 20
> list (map (lambda XRIX)) # Python2.x uses map (lambda XRX))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
Filter (function,sequence): execute function (item) on the item in sequence, and return the item with the result of True as a List/String/Tuple (depending on the type of sequence).
# find the even number between 1 and 20
> list (filter (lambda x:x%2 = = 0MagneRange (1m 21)) # Python2.x uses filter (lambda x:x%2 = = 0m range (1m 21))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Reduce (function,sequence): the number of parameters received by function can only be 2. First, the first and second values in sequence are passed to function as parameters, and then the return value and third value of function are passed to function as parameters, and then only one result is returned.
# find the sum of 100
> after from functools import reduce # Python3.x, you need to import reduce module
> reduce (lambda x recordy y x range (1101))
5050
# find the sum of 100, plus 10000
> reduce (lambda x recordy y recupery range (1101), 10000)
15050
(3) closure
Closure: a function defined inside a function that allows variables to be accessed even if they are outside the scope of the function.
Let's look at an example of using the lambda function as a closure.
Def add (n):
... Return lambda x:x+n
...
> add2 = add (5)
Add2 (15)
twenty
4. The advantages and disadvantages of Lambda function.
Advantages:
(1) the lambda function is relatively portable, the code is concise, and no additional variables are added. Delete-and-use is a good fit to complete a function, but this feature is only used in this place.
(2) Anonymous functions, which are generally used for functional programming services such as filter and map
(3) as a callback function, it can be passed to some applications, such as message processing, immediately (without variables).
Disadvantages:
The Lambda function is a function that can take any number of parameters, including optional parameters, and return a single expression value. Compared with many other languages, Python has many restrictions on Lambda, the most serious of which is that it can only be made up of one expression. This restriction is mainly to prevent abuse, because when people find lambda convenient, it is easier to abuse, but using too much will make the program look less clear, after all, everyone has different levels of patience and understanding of the level of abstraction.
At this point, the study of "what is the use of the Lambda function" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.