Lambda, map, reduce, filter in Python

Source: Internet
Author: User

filter (function, sequence): Executes function (item) on item in sequence, and the item that executes the result of true is composed of a list/string/ Tuple (depending on the type of sequence).

Filter (function or None, sequence), list, tuple, or string: entry is function and list/tuple/string, return value is Item List/tuple/string.

map (function, sequence) : Executes function (item) on item in sequence, and the execution result function (item) is made into a list to return.

Map (function, sequence[, sequence, ...]), List: The entry parameter is for the function and list/tuple/string, and the return value is a function (item).

reduce (function, sequence, starting_value): Invokes function for the item order in sequence, and can be called as an initial value if there is a starting_value. function can receive only 2 parameters, the first value in sequence and the second worth argument to function, and then the return value of function and the third worth parameter to function, and then return only one result.

Reduce (function, sequence[, initial]), value: The entry is a function and a list/tuple/string and an initial value, and the return value is numeric.

#Coding=utf-8" "Created on 2016-12-14@author:jennifer Project: Usage of filter, map, reduce, lambda in Python" "#1.LAMBDA usage, before the colon is the argument, after the colon is the expression, the return value, the simplest functionPrint[(LambdaX:X*X) (x) forXinchRange (11)]#results: [0, 1, 4, 9, (+) , +/-, +, +, Bayi, +]Print(LambdaX:X*X) (3)#Results: 9g=Lambdax:x*xPrintG (4)#Results:#2.filter Usage: Returns the argument that the execution result is true (the entry parameter is a list word OFDM group)PrintFilterLambdaX:x*x-4,range (10))#results: [0, 1, 3, 4, 5, 6, 7, 8, 9]#3.map Usage: Executes the function sequentially on the list entry parameter. The entry is a list of how many lists there should be. PrintMapLambdaX:x*x-4,range (10))#results: [ -4, -3, 0, 5, A, (+), +,--)PrintMapLambdaX,y:x*y-4,range (3), [8,9,10])#results: [ -4, 5, +]#4.reduce usage: First pass the first value in sequence and the second worth parameter to function, then pass the function return value and the third worth parameter to Fuction, and finally return a result value#the number of incoming parameters can only be 2PrintReduceLambdaX,y:x*y-4,range (4))#results: -40#calculates 0 to 100 of the andPrintReduceLambdaX,y:x+y, Range (101))#Results: 5050PrintReduceLambdaX,y:x+y, Range (101), 100)#results: 5150

Transfer from https://www.cnblogs.com/yufeihlf/p/6179982.html

Lambda, map, reduce, filter in Python

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.