Python Learning Record

Source: Internet
Author: User

Python function record

map () takes a sequence of functions and a sequence, and then preserves the result returned by the function

reduce () receives a function and a sequence function result as a value, and the operation is passed from the first value of the sequence to the function, returning the result to the function again until the result is finally obtained.

The filter () filtering function receives a function and a sequence and then the function returns True and false, preserving the return true in the sequence.

def is_odd (n):    return n% 2 = = 1filter (is_odd, [1, 2, 4, 5, 6, 9, 10, 15])
# results: [1, 5, 9, 15]

The sorted () sort function can customize the comparison function.

pp = [2,10,-5,100,25,-50]p = sorted (PP) print P

You can also define a comparison function like this

DEF CMP (x, y):    if x>y:        return-1    if x<y:        return 1    return 0pp = [2,10,-5,100,25,-50]p = sorted ( PP,CMP) Print P

Lambda anonymous function Call method Lambda x:x * x


Python Learning Record

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.