Written in the book before

Today I’m going to introduce you to a few special functions that are characteristic of functional programming, and some people see them as proof that Python can be “functional programming.” What functional programming is is not the focus of this article, so you’re interested in learning about it. As regular readers may know, I’m a big fan of Python’s simplicity and elegance, and the best thing about these functions today is that they make your program cleaner, although you can do it in other ways without them.

Lambda functions

Lambda is a function that can solve a problem in a single line. Let’s look at the following example:

>>> def add(x):
...     x += 1
...     return x
...
>>> numbers = range(5)
>>> list(numbers)
[0, 1, 2, 3, 4]
>>> new_numbers = []
>>> for i in numbers:
...     new_numbers.append(add(i))
...
>>> new_numbers
[1, 2, 3, 4, 5]
Copy the code

In the example above, the function add() acts as an intermediate role, but the example above can also be implemented as follows:

>>> new_numbers = [i+1 for i in numbers]
>>> new_numbers
[1, 2, 3, 4, 5]
Copy the code

First of all, let me say that the above list parsing is actually very useful, but I chose to use lambda instead of add(x) :

>>> lamb = lambda x: x+1
>>> new_numbers = []
>>> for i in numbers:
...     new_numbers.append(lamb(i))
...
>>> new_numbers
[1, 2, 3, 4, 5]
Copy the code

In this case, lamb is equal to add(x), and lamb = lambda x: x+1 is equal to the code block in add(x). Here are a few more small examples of lambda applications:

>>> lamb = lambda x,y: x + y >>> lamb(2) 3 >>> lamb1 = lambda x: x ** 2 >>> lamb1(5) 25Copy the code

From the above example, we can summarize the specific use of the lambda function: the variable is directly followed by the lambda, the colon is followed by the colon, and the expression is evaluated as the return value of the function.

One caveat here is that while lambda functions can take as many arguments as they want and return the value of a single expression, lambda functions cannot contain commands and cannot contain more than one expression. If you need something more complicated, you should define a function.

Lambda, as a one-line function, can be used as an option in your specific programming practices, and while it doesn’t increase performance, it looks comfortable.

The map function

We used the lambda example above, but map can also be implemented, see the following operation:

> > > Numbers =,1,2,3,4 [0] > > > map (add, Numbers) [1, 2, 3, 4, 5] > > > map (lambda x: x + 1, Numbers) [1, 2, 3, 4, 5]Copy the code

Map is a built-in function in Python. Its basic format is map(func, seq).

Func is a function object, and seq is a sequence object. At execution time, each element in seq is pulled from left to right, inserted into func, and the return value of func is stored in a list.

It is important to understand the following points about map:

1. Use fun (essentially a for loop) for each element of an iterable in turn.

2. Return all the results as a Map object, which is an iterator.

Let’s do a simple little problem: add the corresponding items from two lists and return the result in one list. We do this using a map. If you are done, read on:

> > > list1 = [1, 2, 3, 4] > > > list2 =,6,7,8 [5] > > > list (map (lambda x, y, x + y, list1, list2)) [6, 8, 10, 12]Copy the code

If you look up there, isn’t that easy? Map: Map: map: for: map

> > > list1 = [1, 2, 3, 4] > > > list2 =,6,7,8 [5] > > > list3 =,10,11,12 [9] > > > list (map (lambda x, y, z: x + y + z,list1,list2,list3)) [15, 18, 21, 24]Copy the code

How about three? Is it a little bit troublesome to use for? So we’re thinking what if it’s four or five or more? This shows the neat elegance of maps, and the performance benefits of maps are not the same as those of lambda.

The filter function

Filter translates to “filter,” and in Python, it does. This is a little cumbersome to explain, or directly on the code is good, in the code to understand the usage is I have been reflected in all the articles:

>>> numbers = range(-4,4)
>>> list(filter(lambda x: x > 0,numbers))
[1, 2, 3]
Copy the code

The above example is actually equivalent to the following code:

>>> [x for x in numbers if x > 0]
[1, 2, 3]
Copy the code

Then let’s write an example to see how it works:

>>> list(filter(lambda x: x ! ='o'.'Rocky0429'))'R'.'c'.'k'.'y'.'0'.'4'.'2'.'9']
Copy the code

The reduce function

As I’ve said many times in previous articles, my code is in Python3. In Python3, reduce functions are placed in the functools module, and in Python2 they are still in the global namespace.

Again, let’s run with an example. Let’s see how it works:

Reduce (lambda x,y: x+y,[1,2,3,4]) 10Copy the code

The first argument to the reduce function is a function, and the second argument is an object of type sequence, which applies the functions to the sequence from left to right. If you don’t understand, here’s how it differs from map:

> > > list1 = [1, 2, 3, 4] > > > list2 =,6,7,8 [5] > > > list (map (lambda x, y, x + y, list1, list2)) [6, 8, 10, 12]Copy the code

Comparing the two examples above, we can see the difference between the two. Map is calculated from the top and bottom, while Reduce is calculated from the left to the right.

Written in the book after

So far, I’ve introduced four functions that not only make the code simpler, but also optimize their performance in Python3. So if you like it, feel free to use it.

For more content, please follow the public account “Python Space”, looking forward to communicating with you.