In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what are the application scenarios of lambda expressions in Python", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn what are the application scenarios of lambda expressions in Python.
01
Lambda expression
With its simple writing and powerful functions, python has rapidly developed into the main language of AI and deep learning. Introduce the lambda expression in Python and notice that it is only an expression, not a statement.
The syntax rule of lambda: one or more parameters terminate the input parameters with colons, which can be understood as the input parameters of the prototype of the named function, followed by an expression, which is equivalent to the function body part of the named function. The function body part of lambda is the output part as the return value.
02
Scenarios 1:map and reduce
Map and reduce in Python, combined with lambda expressions, are more efficient to use, as follows:
Def sum (mylist):
Return reduce (lambda x, y: 2% x + 1% y, mylist)
You can see that the two input parameters, x, y, are returned as 2% x + y, thus simplifying the elements of the mylist.
03
Scenario 2: lambda and list are used together
This scenario is often asked in interviews, as shown below:
Li = [lambda: x for x in range (10)]
Get a list of lambda, print what li [0] () will get, notice this calling method, because it is an array, take the first element, li [0], followed by parentheses to indicate the call to the lambda expression. Is the printed result 0?
NO! It's 9
The lambda expression does not form a memory of the variables in the function body, but only records the last state.
So if the input parameter of lambda has x, will we get the desired result 0?
Li = [lambda x: x for x in range (10)]
Res = li [0] (0)
Print (res)
Output: 0
The above is all the content of the article "what are the application scenarios of lambda expressions in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.