Function
When the return value of a function is the function name of another function, only the memory address of the function is returned, and the scope of the function does not change.
Name ='WINSODM'defTest (): Name='XL' Print('In the test')deftest1 ():Print('In the test1') returnTestres=test1 () res ()#The result is:Name ='XL''In the test'#The test returned here runs after the value of the variable name in test, not the name= ' winsdom ' of all variables.
anonymous functions
Lambda keyword
Format: Lambda parameter: return value
Lambda x:x+1lambda x, Y, Z: (x+1,y+1,z+1)
Anonymous function that automatically frees up memory space when it is finished.
Higher order functions
1. Pass the function as a parameter to another function (the parameter received by the function is a functional name)
2, the return value contains the function
A function that conforms to any of the two rules above is called a higher order function.
def Test (): ' XL ' Print ('in thetest') # Non-higher order functions def test1 (): Print ('in thetest1') # Higher order functions return Test
Map built-in functions
Passing in a function, an iterative object, traversing an iterative object, using an iterative object as an incoming function to process and returning the result of the operation
num_l = [1,3,5,2,10,4,6,9,8]# defines a list =list (map (Lambda x:x+1,num_l))# Pass in a 1 anonymous function and a list, and convert it to list form to assign a value to the new list of new_num_l # The result is: Print = [2, 4, 6, 3, 11, 5, 7, 10, 9]
Achieve yourself:
num_l = [1,3,5,2,10,4,6,9,8]#Define a listdefAdd_one (x):#define a function that has an increment of 1 returnX +1defmap_1 (fucn,l): new_num_l= []#Create a new list forIinchL:new_num_l.append (FUCN (i))#Traverse num_l, increment by 1 using the incoming function, and add to the new list new_num_l returnnew_num_l#return to New listnew_num_l = Map_1 (add_one,num_l)#receive a new list#new_num_l = map_1 (lambda x:x+1,num_l) #也可以直接传入匿名函数Print(new_num_l)#The result is:new_num_l = [2, 4, 6, 3, 11, 5, 7, 10, 9]
Example: Using a map built-in function to convert a string to a list and all uppercase
Name ='Winsdom'name_list= List (Map (LambdaX:x.upper (), name))Print(name_list)#The result is:Name_list = ['W','I','N','S','D','O','M']
Filter built-in functions
Pass in a function, an iterative object, iterate over an iterative object, make a bool value for each element in an iterative object, and if it is ture, keep it.
Name_list = ['XLC','HHC','HZZC','HC','Winsdom']Print(List (Filter (LambdaX:x.endswith ('m') , Name_list )))#The result is:['Winsdom']
Name_list = ['XLC','HHC','HZZC','HC','Winsdom']Print(List (Filter (LambdaX: notX.endswith ('C') , Name_list )))#The result is:['Winsdom']
Achieve yourself:
Name_list = ['XLC','HHC','HZZC','HC','Winsdom']defEnd_with (x):#defines a function that determines whether to end with C returnX.endswith ('C')deffilter_1 (Func,array): New_name_list= []#Create a new list forNincharray:if notFunc (N):#decide to add a new list if it doesn't end with Cnew_name_list.append (n)returnnew_name_listnew_name_list=filter_1 (end_with,name_list)#new_name_list = filter_1 (lambda x:x.endswith (' C '), name_list) #用匿名函数直接传入, because the filter_1 wrote not so the anonymous function here does not need to write notPrint(new_name_list)#The result is:New_name_list = ['Winsdom']
Reduce built-in functions
Pass in a function, an iterative object, a default parameter that compresses an iterative object into a value to return
Reduce can be used directly in Python2 and import is required in Python3
from Import = [1,3,5,100= reduce (lambda x,y:x*y,num_l,2)print(num)# The result is:num = 3000
Achieve yourself:
fromFunctoolsImportreducenum_l= [1,3,5,100]defReduce_1 (Func,array,init = None):#Init represents an initial value ifinit = =None:res=array.pop (0)Else: Res=Init forIinchArray:res=func (res,i)returnResres= Reduce_1 (Lambdax,y:x*y,num_l,100)Print(RES)
Python functions, anonymous functions, higher-order functions, built-in functions--08