Python Basics-Functional programming

Source: Internet
Author: User

The three functions built into Python are useful for use with sequences: Filter (), map (), and reduce ().

1.filter (function, sequence)

filter()is to sift out the qualifying elements from a sequence.

filter()It also receives a function and a sequence that filter() acts on each element in turn, and then decides whether to True False retain or discard the element based on the return value.

Because filter() lazy computing is used, only the results are filter() actually filtered and each time the next filtered element is returned.

For example, to get a number divisible by 3 or 5.

def f (x):    return x 3 = = 0 or x 5 = = 0print Filter (f, Range (2, 25)) # [3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24]

For example, delete an empty string in a sequence

def not_empty (s):    return S and S.strip () print filter (Not_empty, [' A ', ' B ', ' ', ' C ', None, ' d ']) # [' A ', ' B ', ' C ', ' d ']

2.map (function, sequence)

mapFunctions the passed-in function sequentially to each element of the sequence and returns the result

def cube (x): Return x*x*xprint Map (cube, range (1, one)) # [1, 8, 8, 216, 343, 729, 1000]seq = Range () def add (x, y): Return x+yprint map (add, seq, seq) # [0, 2, 4, 6, 8, 10, 12, 14]

You can also use map to convert a set of numbers into strings.

List (map (str, [1, 2, 3, 4, 5, 6, 7, 8, 9]) # [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ']

3.reduce (function, sequence)

This function must receive two parameters, the reduce result will continue and the next element of the sequence to do the cumulative calculation, the effect is:

Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)

For example, for 1 to 100, you can use reduce to implement

def add (x, y): return x+yprint reduce (add, range (1, 101)) # 5050

In this way, the sequence is obtained, and if the sequence is empty, an exception is thrown. To avoid throwing exceptions, you can use the following method

def sum (seq):    def add (x, y): return x+y    return reduce (add, seq, 0) Print sum (range (1, one)) # 55print sum ([]) # 0

Python Basics-Functional programming

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.