Python filter, map, reduce, lambda

Source: Internet
Author: User

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

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.