First, anonymous function |
An anonymous function is one that does not require explicit specified function names , and the most complex operation supported by anonymous functions is the ternary operation.
anonymous function functions:
1. Save Code Volume
2, looks more advanced
# Example One: def Calc (x, y ): return x*y# overwrites the above general function as an anonymous function:lambda x,y:x*Lambda X,y:x*y # declares an anonymous function and assigns a value to the Funcprint(func (3,8)) # output of 24
#Example Two:#To overwrite a complex function with an anonymous functiondefCalc (x, y):ifX <y:returnx*yElse: returnx/yPrint(Calc (16,8))#converting the above function into an anonymous function, the most complex operation supported by an anonymous function is the ternary operation. func1=LambdaX,y:x*yifX < yElseX/Y#anonymous function converted to ternary operation formPrint(Func1 (16,8))
Second, anonymous function application |
data = List (range (10))Print(data)#method One: forIndex,iinchEnumerate (data): Data[index]= i*IPrint(data)#Method Two:defF2 (n):returnNNPrint(List (map (F2,data )))#Method Three:Print(List (Map (LambdaX:x*x,data)))#map functions and anonymous functions are used once, so no need to define functions
Python anonymous functions