In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what function objects does Python have". In daily operation, I believe many people have doubts about what function objects Python has. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what function objects Python has". Next, please follow the editor to study!
Lambda function
Before we expand, let's mention the lambda function. You can define a function using the syntax of the lambda function. Examples of lambda are as follows:
Func = lambda XBI y: X + yprint func (3Pol 4)
Lambda generates a function object. The parameter of this function is xQuery y, and the return value is xcorrecy. The function object is assigned to func. The call to func is no different from a normal function.
The above definition can be written in the following form:
Def func (x, y): return x + y function is passed as a parameter
The function can be passed as an object for arguments. The name of the function (such as func) is the object. For example:
Def test (f, a, b): print 'test' print f (a, b) test (func, 3,5)
The first argument f of the test function is a function object. Pass func to f () in fMagee test and you have the function of func ().
As a result, we can improve the flexibility of the program. You can use the test function above to take in different function parameters. For example:
Test ((lambda x _ ray y: X _ ray _ 2 + y), 6,9) map () function
Map () is the built-in function of Python. Its first argument is a function object.
Re = map ((lambda x: Xerox), [1meme 3pr 5je 6])
Here, map () has two parameters, a function object defined by lambda and a table containing multiple elements. The function of map () is to act on each element of the table in turn, and the result of each action is stored in the returned table re. Map manipulates the data by reading the function (in this case, the lambda function) (where "data" is every element in the table, and "operation" is adding 3 to each data).
In Python 3.x, the return value of map () is a circular object. You can use the list () function to convert the circular object into a table.
If the function object as a parameter has more than one parameter, you can pass multiple parameters of the function parameter to map () in the following way:
Re = map ((lambda Xrem y: Xray), [1meme 2rem 3], [6rem 7rem 9])
Map () fetches one element at a time from each of the two tables and brings it into the function defined by lambda.
Filter () function
The first argument to the filter function is also a function object. It also acts on multiple elements with a function object as an argument. If the function object returns True, the element for that time is stored in the returned table. Filter filters the data through the functions read in. Similarly, in Python 3.x, filter returns not a table, but a circular object.
The use of the filter function is as follows:
Def func (a): if a > 100: return True else: return Falseprint filter (func, [10Magne56101500]) reduce () function
The first argument to the reduce function is also a function, but there is a requirement that the function itself can take two arguments. Reduce can progressively apply functions to individual parameters. Such as the following example:
Print reduce ((lambda x-ray: X-ray), [1, 2, 5, 7, 9])
The first argument to reduce is the lambda function, which takes two arguments, x _ focus y, and returns x _ quoy.
Reduce passes the first two elements (1 and 2) in the table to the lambda function and gets 3. The return value (3) will be used as the first parameter of the lambda function, and the next element (5) in the table will be the second parameter of the lambda function, and the next call to the lambda function will get 8. The lambda function is called in turn, and each time the first argument of the lambda function is the result of the previous operation, and the second parameter is the next element in the table until there are no remaining elements in the table.
The above example is equivalent to (1x 2) + 5) + 7) + 9
According to the reminder of mmufhy: the reduce () function cannot be used directly in 3.0. it is defined in the functools package and needs to be introduced into the package.
At this point, the study of "what function objects Python has" 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.