(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! '))