anonymous function
1. What is an anonymous function
A lambda is an anonymous function and an expression, the smallest unit of combat of a function
Anonymous functions are, as the name suggests, "nameless" functions.
Anonymous functions for simple business logic
One function per line
In layman's terms, functions with uncomplicated logic that don't require large-scale bulk calls can be built using anonymous functions, which are written in a way that determines their simplicity.
#lambda is simple and uncomplicated
Note that lambda is a python keyword used to declare an anonymous function
** The difficulty of anonymous functions is not in their construction itself, but in their combinatorial use!
2. How to declare/call an anonymous function (lambda)
# Traditional functions: def add(num1): return num1 * 2 call (programming):add(2) exports:4 def add(num1,num2): return num1 + num2 call (programming):add(1,2) exports:3 # anonymous function: result = lambda x: x*2 call (programming):result(2) exports:4 result = lambda x,y: x+y call (programming):result(x=1,y=2) exports:3
3. Several ways to use anonymous functions
#1, anonymous function combining three-eye operations traditional function def compare(x,y): if x > y: return x else: return y call (programming):compare(2,1) exports:2 anonymous function compare = lambda x,y: x if x > y else y call (programming):compare(2,1) exports:2
#2. pass lambda as a method into a traditional function schools = ["Python Basics.", "Python crawler.", "Java Programming", "Java Web", "Python Data Analysis." ] def keyword_serch(keys,func): search_result= [] for result in keys: if func(result): search_result.append(result) return search_result condition = lambda x: True if "Python" in x else False invocations:keyword_serch(schools,condition) exports: ["Python Basics.","Python crawler.","Python Data Analysis."]
#3: How to pass parameters to lambda in def function? def main(): return lambda x: True if x == 10 else False (in the event thatxbe tantamount to10Return to true,Otherwise false) invocations:main()(10) fulfillmentmainfunction (math.),stillmainNo need to pass a parameter; The second bracket is then for thelambdatransmittedxspecifications exports:True
#4. pass lambda as a method into a traditional function def income(basic,transport,phone): return lambda x: x+basic+transport+phone invocations:total = income(1000,2000,3000) total(4000) exports:10000 schools = ["Python Basics.", "Python crawler.", "Java Programming", "Java Web", "Python Data Analysis." ] def keyword_search(keys,func): search_result= [] for key in keys: if func(key): search_result.append(key) return search_result def search_condition(c): return lambda x: True if c in x else False python = search_condition("Python") invocations:keyword_search(schools,python) exports:["Python Basics.","Python crawler.","Python Data Analysis."]
4. Combined use of anonymous functions
map function
filter function
reduce function
sorted function
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!