Python built-in functions

Source: Internet
Author: User

Lambda function:in a lambda statement, the colon is preceded by a parameter, can have multiple, separated by commas, and the return value to the right of the colon

1. Map/reduce function

(1)map()The function receives two parameters, one is a function, the other is a sequence, the map incoming function functions sequentially to each element of the sequence, and returns the result as a new list

""= map (lambda x:x*x, [1, 2, 3, 4, 5, 6, 7, 8, 9])print lis T
""= map (str, [1, 2, 3, 4, 5, 6, 7, 8, 9])print list

(2) reduce () function receives two parameters, one is a function, one is a sequence, and the elements in the sequence return a result by processing a two-tuple function, reduce The result continues and the next element of the sequence is calculated as a cumulative

"'"= reduce (lambda x, y:x * y, [1, 2, 3, 4, 5])print result 

2. Filter function: The function receives two parameters, one is a function, one is a sequence, the elements in the sequence are filtered through the function to return a new list

"'" = Filter (lambda x:x% 2 = = 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]) C13>print List

3. Sorted function: The function can sort the list, it can also receive a comparison function to implement a custom sort

" "to sort a list" "List= Sorted ([1, 2, 3, 4, 5, 6, 7, 8, 9], reverse=False)PrintList" "use a custom function to sort the list in reverse order" "defreversed_cmp (x, y):ifX >y:return-1ifX <y:return1return0list= Sorted ([1, 2, 3, 4, 5, 6, 7, 8, 9], reversed_cmp)PrintList

Python built-in functions

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.