Several of the functions built into Python are simple examples of filter, map, reduce, and lambda.
#!/usr/bin/env python#_*_coding:utf-8_*_#filter (function, sequence):#a function (item) is executed on item in sequence, and the item that executes the result of true consists of a list/string/tuple (depending on the type of sequence) returned. #can be seen as a filter function. tasks = [ { 'ID': 1, 'title': U'Golang', 'Description': U'Golang', ' Done': False}, {'ID': 2, 'title': U'Learn Python', 'Description': U'need to find a good Python tutorial on the Web', ' Done': False}]defGet_task (task_id): Task= Filter (Lambdat:t['ID'] ==task_id, Tasks)PrintTaskget_task (2)#map (function, sequence)#executes function (item) on item in sequence, and the result of execution consists of a list returndefAdd (x, y):returnX +yPrintMap (add, Range), range (10))PrintMapLambdaX,y:x+y, Range (10), range))def_str (s): s= S +". Log" returnSS= ["a","b","C"]Printmap (_str, s)#reduce (function, sequence, starting_value)#The function is called on an iteration of the item order in sequence, and if there is starting_value, it can also be called as an initial value, for example, to sum the listPrintReduce (Add, range (1, 10))
The result is:
[{'Description': U'need to find a good Python tutorial on the Web',' Done': False,'ID':2,'title': U'Learn Python'}][0,2,4,6,8,Ten, A, -, -, -][0,2,4,6,8,Ten, A, -, -, -]['A.log','B.log','C.log'] $
Python filter, map, reduce, lambda