Pythont Special Grammar filter,map,reduce,apply use method introduction

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:

function name = Lambda ARGS1,ARGS2,..., argsn:expression

For example:

Add = Lambda x,y:x + yprint add

(2) Filter

The filter function is equivalent to a filter, and the function prototype is: Filter (function,sequence), which indicates that each element in the sequence sequence executes function in turn, where function is a bool function, for example:

sequence = [1,2,3,4,5,6,7,8,9,10]fun = lambda x:x% 2 = = 0seq = 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:

def filter (FUN,SEQ):    filter_seq = [] for    item in SEQ:        if Fun (item):            filter_seq.append (item)    Return FILTER_SEQ

(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:

Seq = [1,2,3,4,5,6]fun = lambda x:x << 2print map (FUN,SEQ)

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

def map (fun,seq):    mapped_seq = [] for    item in SEQ:        mapped_seq.append (item)    return MAPPED_SEQ

(4) Reduce

The reduce function is in the form of reduce (function,sequence,initval), which represents a two-tuple function, sequence represents the sequence to be processed, and Initval represents the initial value of the processing. Like what:

Seq = [1,2,3,4,5,6,7,8,9,10]fun = lambda x,y:x + 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

The source code for the reduce function is roughly as follows:

def reduce (fun,seq,initval = None):    lseq = list (seq)    If Initval is None:        res = lseq.pop (0)    else:        Res = Initval for    item in LSEQ:        res = fun (seq,item)    return res

(5) Apply

Apply is used to indirectly replace a function, such as:

Def say (A, b):    print a,bapply (say, (234, ' Hello world! '))

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.