anonymous function lambda
function body: Lambda parameter: return value
1 Lambda x:x*22print(ret (2))
Built-in functions that work with anonymous functions
1.map---------Change the return value according to the parameters
1Example: There is a list "'a ','B','C'"Use anonymous functions to add a _SB to the parameters, for example2 A_SB3 4L = ['a','b','C']5RET = Map (LambdaX:x +'_SB', L)6 Print(List (ret))
2.filter---------Handsome Choice
1 Example: There is a number "1,2,3,4,5,6,67,78," Using anonymous function Courtship number 3 l = "1,2,3,4,5,6,67,78,"4 ret = filter (lambda x:x% 2 = = 0, l)5Print(list (ret))
Homework:
1there is a file with a total of more than 20 lines, each 5 behavior one page, depending on the number of pages entered by the user, returns the row count of the specified page with open ('file', encoding='Utf-8') as f: Read file ret=F.readlines () a one-piece reading Pan_num= Int (Input ('Please enter the number of pages to display') gets the number of pages entered by the user, converted to the INT type pan, num= Divmod (len (ret), 5) using the take-off function,ifNum:pan+=1 if the number of surplus, page +1ifPan_num >Pan:Print('wrong number of pages entered')elifPan_num ==pan andNum! =0: If the user enters a number of pages equal to the number of text pages and the remainder is not 0 forIinchrange (num): This is the remainder, because only the remainder of the output,Print(ret[(pan_num-1) *5+I].strip ())Else: forIinchRange (5): Range 5 because one page is 5 rowsPrint(ret[(pan_num-1) *5+i].strip ())
Anonymous functions and several and anonymous functions cooperate with each other