Python functional programming zip map filter reduce

Source: Internet
Author: User
Tags zip in python

Python has many functional programming methods. This article describes the most common functions.

Note: This article is based on python2.7. different versions may cause differences.

Zip-funding

Standard format zip (seq [, seq [, seq])

Example:


A = (I for I in range (3 ))
B = [4, 5, 6]
C = (7, 8, 9)
Abc = zip (a, B, c)

The result of abc is [(0, 4, 7), (1, 5, 8), (2, 6, 9)]

Therefore, the role of the zip function is already very obvious. It is like raising funds from three abc companies.
What should I do if c is rich and a is not so rich?


A = (I for I in range (2 ))
B = [4, 5, 6]
C = (7, 8, 9, 10)
Abc = zip (a, B, c)

The result of raising funds from three abc companies is [(0, 4, 7), (1, 5, 8)]. If you say that one of them is out, your family will be gone, then I will not go out (you see, the code is so realistic ...)
The reason why not all the chestnuts are list is to illustrate a small point of attention. zip accepts parameters with iterative attributes (_ iter _), which will be explained later.
Filter-review

Standard format filter (func, seq)

Example:


A = [1, 2, 3]
B = [4, 5, 6]
C = [7, 8, 9]
Result = filter (lambda x: sum (x)> 14, [a, B, c])
Result: [[4, 5, 6], [7, 8, 9]

There is now a project, but this project requires an asset review, requiring enterprises whose assets meet lambda standards to be eligible to participate in the tender (func is a bool function)
Map-bidding

Standard format map (func, seq [, seq [, seq])

Example:


A = (I for I in range (3 ))
B = [4, 5, 6]
C = (7, 8, 9)
Abc = map (lambda x, y, z: x + y + z, a, B, c)

The abc result is [11, 14, 17].

In this bidding project, lambda functions need to recruit three (number of function parameters) to participate in this project. This project is divided into the investment of the first, middle, and third phases.
If the three families of abc are lucky to be selected, and all three have prepared three stages of investment. therefore, in the early stage, a has 0, B has 4, and c has 7 invested in the lambda project. In the early stage, the project has returned 11 revenues.

And so on, until all three of them end, the returned income is [11, 14, 17].

If c has money (c = [7, 8, 9, 10, 11]), c said: Lao Tzu has money and I want to invest more. What should I do if AB has no money? Simple !! AB honestly said: I don't have money. (a = [, 2, None, None], B = [, 6, None, None])

Therefore, the lambda project receives such input [None, None, 10], [None, None, 11]. Pay attention to processing these None here, because it will report NoneType if it is not careful.

A = (I for I in range (3 ))
B = [4, 5, 6]
C = (7, 8, 9, 10, 11)
Abc = map (lambda x, y, z: str (x) + str (y) + str (z), a, B, c)
Therefore, the abc result is ['047', '1234568', 'nonenone10', 'nonenone11'].

Map also accepts parameters with iterative attributes.

Note that when func is None, the map function is similar to zip. in the end, map is the first fund-raising (zip). When the func does not exist, the fund-raising (zip) is left.
Reduce-secondary investment

Standard format reduce (func, seq [, init])

Example:


A = (I for I in range (3 ))
Result = reduce (lambda x, y: x + y,)
Result is 3
The reduce workflow is as follows:
1. func (seq [0], seq [1])-> applies the first and second elements of seq to func
2. func (seq [0], seq [1]), seq [2])-> the result of the previous func is used as the first parameter of func, seq, the third parameter, serves as the second element of func.
......
N. func (... func (seq [0], seq [1])..., seq [n])-> the result of func in the previous step is used as the first parameter of func, and the nth parameter of seq is used as the second element of func.
The following figure shows the process:


                   
If init does not exist, when init exists, the first parameter of func in the first step is init, and the first element of seq is the second parameter of func, and the subsequent process is the same.
This is like making a first-stage profit as a secondary investment capital.
Conclusion
In python, function programming can save a lot of development time, and it will be our own utility.
The content mentioned in this article may be inappropriate. After all, I am not an economic professional .... Haihan

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.