Anonymous functions and several and anonymous functions cooperate with each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.