Python special Syntax--filter, map, reduce, lambda

Source: Internet
Author: User

The  filter (function, sequence) executes function (item) on item in sequence, and the item that executes the result of true makes up a list/string/the Tuple (depending on the type of sequence) returns:deff (x):returnX% 2! = 0 andX% 3! =0PrintFilter (F, Range (2, 25)) #[5, 7, one, all, +, +]defF1 (x):returnX! ='a' PrintFilter (F1,"abcdef")#Bcdef Second, map (function, sequence) executes function (item) on item in sequence, and the result of execution consists of a list return:defcube (x):returnx*x*xPrintMap (cube, range (1, 11)) #[1, 8, 216, 343, 729 , +]defcube1 (x):returnX +xPrintMap (CUBE1,"ABCDE") #[' AA ', ' BB ', ' cc ', ' dd ', ' ee ']In addition, the map supports multiple sequence, which requires that the function also supports the corresponding number of parameter inputs:defAdd (x, y):returnx+yPrintMap (Add, Range (8), range (8)) #[0, 2, 4, 6, 8, ten, +]Status_ids= Map (LambdaX:x.get ('status__id'), snaps) three, reduce (function, sequence, starting_value) calls function for the item order in sequence, if there is starting_value can also be called as an initial value, for example, you can use to sum a list:defAdd (x, y):returnX +yPrintReduce (Add, range (1, 11)) Note:1+2+3+4+5+6+7+8+9+10reduce (Add, range (1, 11), 20) Note:1+2+3+4+5+6+7+8+9+10+20 iv.Lambda This is Python support an interesting syntax that allows you to quickly define a single line of the smallest function, similar to the C language of the macro, these functions, called Lambda, is borrowed from Lisp, can be used in any need function of the place: G=LambdaX:X * 2PrintG (3)#6Print(LambdaX:X * 2) (3)#6 Five, sortedAA= [   {'name':'Zhangsan',' Price': 20.01,'Date':'2015-01-09t01:00:00z'},   {'name':'Lisi',' Price': 10.01,'Date':'2013-01-09t01:00:00z'},   {'name':'Wangwu',' Price': 0.01,'Date':'2012-01-09t01:00:00z'}] Sorted (AA, Key=LambdaS:s.amount)#to sort a listSorted (AA, key=LambdaS:s.amount, reverse=True) AA= [<symbol:symbol object>, <symbol:symbol object>, <symbol:symbol object>] Sorted (AA, Key=Lambdas:s["Date"])#sort the symbol object, date is the symbol propertySorted (AA, key=Lambdas:s["Date"], reverse=True) Vi. for special usage  forIinchRange (4):    PrintIse=[x**2 forXinchRange (4)]PrintSe#[0, 1, 4, 9]SE=[x**2 forXinchRange (10)if  notx%2]PrintSe#[0, 4, approx.

Python special Syntax--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.