Lambda-filter-reduce-apply-map of Python built-in functions

Source: Internet
Author: User

    • (1) Lambda

Lambda is a useful syntax in Python that allows you to quickly define a single-line minimum function. Similar to a macro in C, it can be used wherever a function is needed.

The basic syntax is as follows:

函数名 = lambda args1,args2,...,argsn : expression

For example:
Add = Lambda x,y:x + y
Print (ADD)

    • (2) Filter

filterfunction is equivalent to a filter, the function prototype is: filter(function,sequence) , for each element in the sequence sequence execution function, where function is a bool function, for example:

= [1,2,3,4,5,6,7,8,9,10=lambda%2==0=filter(fun,sequence)print(seq)

The following code indicates that all the even numbers in the sequence are filtered out.

The filter function prototype is roughly as follows:

deffilter(fun,seq):    = []    forin seq:        if fun(item):            filter_seq.append(item)    return
    • (3) Map

The basic form of map is: Map (function,sequence), which functions the function on the sequence sequence and returns a final result sequence. Like what:

= [1,2,3,4,5,6=lambda<<2printmap(fun,seq)

The map's function source code is roughly as follows:

defmap(fun,seq):    = []    forin seq:        mapped_seq.append(fun(item))    return(mapped_seq)
    • (4) Reduce

The reduce function is in the form of:

reduce(function,sequence,initVal)

functionRepresents a two-tuple function that represents the sequence sequence to be processed, and initVal represents the initial value of the processing. Like what:

= [1,2,3,4,5,6,7,8,9,10=lambda+ yprint(reduce(fun,seq,0))

Indicates that each element in the sequence SEQ starts with an initial value of 0, so the resulting result is 55

reduceThe source code for the function is roughly as follows:

defreduce=None):    =list(seq)    ifisNone:        = Lseq.pop(0)    else:        = initVal    forin Lseq:        = fun(seq,item)    return res
    • (5) Apply

applyis used to indirectly replace a function, such as:

def say(a,b):    print(a,b)apply(say,(234,‘Hello World!‘))

Lambda-filter-reduce-apply-map of Python built-in functions

Related Article

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.