Python built-in functions filter,map and reduce

Source: Internet
Author: User

Python built-in functions filter,map and reduce, three functions are similar, are applied to the sequence of functions, the common sequence includes list, tuple, str and so on. and three functions can be used in conjunction with a lambda expression. The following are described separately.

1. Filter

Filter (BOOL_FUNC,SEQ): This function functions like a filter. Call a Boolean function Bool_func to iterate through the elements in each SEQ, and return a sequence of elements that enable BOOL_SEQ to return a value of true.

For example: Get a sequence that is divisible by 3 from the [1,2,3,4,5,6,7,8,9] sequence

Print filter (lambda x:x%3 = = 0,[1,2,3,4,5,6,7,8,9])

The results are: [3, 6, 9]

2. Map

map (func,seq1[,seq2 ...]) : The function func acts on each element of a given sequence and provides the return value with a list, and if Func is none,func as an identity function, returns a list of n tuples containing the set of elements in each sequence.

Print map (lambda x, y:x * y, [1, 2, 3], [4, 5, 6])

Results: [4, 10, 18]

Print map (lambda x, y: (x * y, XY), [4, 5, 6], [3, 2, 1])

Results: [(12, 1), (10, 3), (6, 5)]

Print map (Lambda x:x * 3,[1,2,3,4,[3,2,1])

Results: [3, 6, 9, 12, [3, 2, 1, 3, 2, 1, 3, 2, 1]] because [3,2,1] is a subsequence, multiplied by 3, the result consists of three groups [3,2,1]

The use map() of functions, the user entered the non-standard English name, the first letter capitalized, other lowercase canonical name. Input: [‘adam‘, ‘LISA‘, ‘barT‘] , Output: [‘Adam‘, ‘Lisa‘, ‘Bart‘] .

Input =[' Adam ', ' LISA ', ' BarT '

Print map (lambda x:x.capitalize (), input) # here called the string built-in function capitalize (uppercase, the rest lowercase)

Results: [' Adam ', ' Lisa ', ' Bart ']

3. Reduce

reduce (Func,seq[,init]):Func is a two-tuple function that functions func on the elements of a SEQ sequence, each carrying a pair (the previous result and the element of the next sequence), continuously acting on the resulting subsequent result with the existing result and the next value , and finally reduce our sequence to a single return value: If the init value is given, the first comparison will be init and the first sequence element instead of the sequence's first two elements.

For example:

Print reduce (lambda x,y:x * y, [1,2,3,4,5,6])

Results: 720

N=5
Print reduce (lambda x,y:x * Y,range (1,n)) #n的阶乘,

When n=5, the result is: 24

Print reduce (lambda x,y:x * Y,range (1,n), ten) #n的阶乘的10倍

Results: 240

Python built-in functions filter,map and reduce

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.