In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to analyze Lambda, Map and Reduce in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
What I want to share with you today is the Python Anonymous function (anonymous functions), also known as the lambda function. An anonymous function means that the function does not have an explicit function name, because this is what it usually looks like when defining a function in Python: def function_name (argument list): balabalaba. For the time being, functions with function_name are called regular functions, while anonymous functions are called lambda functions. An anonymous function does not have an explicit function name, but it does have an explicit lambda flag, and a function with lambda can be called an anonymous function. In general, people don't want to use anonymous functions (because he
We can't use it! ), but both Python books and online tutorials will talk about this, and it is the beginning of the more basic part, and anonymous functions are more abstract at the beginning, so it is necessary to give you Amway what this hole is! Let's begin the chestnut explanation.
# lambda
"
> def function_1 (x, y):
Return x + y
> function_2 = lambda XBI y: X + y
> > >
> function_1 (1Pol 2)
three
> function_2 (1Pol 2)
three
"
From this chestnut we find that both function_1 and function_2 can be called in the same way.
The difference is that an anonymous function does not have a return statement, and the return value of an anonymous function is the expression it defines.
The life cycle of an anonymous function is in the current line, defined there, and used there, after this village, it will no longer be used. This also implies why an anonymous function does not have a function name, because it will no longer be used, and it is a waste of emotion to choose a name.
# description of map () function
Generally speaking, there are also map and reduce functions that appear at the same time as lambda expressions. Let's take a look at it again.
What the hell is this. You may have heard of the Map-Reduce process in Hadoop, here and that.
The principle is similar. The meaning of map is not easy to translate. To understand it this way, there is a function A, an object B that needs to be calculated. If you map A to B, then you use function A for every element in B.
Next, take a chestnut to calculate the value of the trigonometric function.
Import math
Def function_5 (x):
Return math.sin (x)
> list_1 = [- math.pi/4, math.pi/4]
> map (function_5, list_1)
[- 0.7071067811865475, 0.7071067811865475]
If we use lambda expressions instead of regular functions, then
> map (lambda x: math.sin (x), list_1)
It used to take three lines of code, but now it only takes one line to do it.
# reduce function
If we directly reduce the function just defined, let's see what happens.
> reduce (function_5, list_a)
Traceback (most recent call last):
File "", line 1, in
Reduce (function_5, list_a)
TypeError: function_5 () takes exactly 1 argument (2 given)
TypeError, the type is wrong, it says that function_5 originally only needs one parameter, but you gave two.
Where did you get two? Because we have two elements in our list_1, and that's where it comes from.
The syntax of # reduce is:
Reduce (function, seq)
Reduce evaluates the elements in seq in turn according to the expression of function.
You'd better give me a chestnut.
> reduce (lambda Xrem y: Xrex, [47 pr 11, 42 pr 13])
one hundred and thirteen
13 > > reduce (lambda x, y: X, range (1101))
5050
Just now our map function returned multiple values, while reduce returned only one value. Notice the difference here.
# isn't it easy?
In Python 3, the reduce function is put into the functools module, which requires from functools import reduce if you want to use it.
On how to analyze the Python of Lambda and Map and Reduce to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.