Notes | Functional Programming in Python

Notes | Functional Programming in Python
Title picture: from plxabay

The feature of functional programming is that it allows the function itself to be passed into another function as a parameter, and it also allows a function to be returned.
Functional programming in python mainly uses the following four basic functions:
(1), map()
(2), reduce()
(3), filter()
(4), sorted()
and an anonymous function lambda() Often used in conjunction with them.

One, map

The common calling method of map() function is as follows:

map(function, interable)

The two parameters in the map() function are required. The first parameter is a function name or anonymous function, and the second parameter is an iterable object, such as a list, tuple, etc.
The function implemented by the map() function is to pass the elements in the interable to the function in turn for execution and return the result. In python2.x, the result is returned as a new list, and in python3.x, it returns a map object. You can use the list() method to get the contents of the map object, or you can loop through it.
The following example (python3):

>>> map(lambda x: x*2, [1,2,3,4,5])

<map object at 0x0000013D497E92E8>

>>> list(map(lambda x: x*2, [1,2,3,4,5]))

[2, 4, 6, 8, 10]

>>> for i in map(lambda x: x*2, [1,2,3,4,5]):

...     print(i)

...

2

4

6

8

10

>>>

Of course, map() can still pass in multiple interable objects, as follows:

>>> list(map(lambda x,y: x+y, [1,2,3,4,5], [6,7,8,9,10]))

[7, 9, 11, 13, 15]

>>>

When passing in multiple interable objects, first your function body accepts multiple parameters. For example, if you pass in the above example and pass in two interable objects, the function accepts two parameters, namely x, y. When multiple interable objects are passed in, the function takes values ​​from the interable objects in turn, then forms a new tuple, and finally passes the tuple into a function. As in the above example, the first time (1,6) is passed in The function gets the result 7, the second time (2,7) gets the result 9, and so on.

Two, reduce

In python3, reduce() has been removed from the global namespace. It is now placed in the functools module. To use it, you need to import the module first. The common call methods of reduce() are as follows:

from functools import reduce

reduce(function, interable)

The first parameter is a function name or anonymous function, the second parameter is an iterable object, and the third parameter is an initial value (optional parameter), and its function is to return the calculation result of the function. as follows:

>>> from functools import reduce

>>> reduce(lambda x,y: x+y, [1,2,3,4,5])

15

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

120

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

12000

The working principle of reduce() is to take an element in the interable object in turn, calculate with the last result, and finally return the result. If no default parameters are passed in, the first parameter will be the result of the first calculation, and the value will be passed from the second parameter. If there are default parameters, calculate directly from the first parameter and the default value, and then continue.

Three, filter

The calling format of the filter() function is as follows:

filter(function, iterable)

The two parameters of the filter() function, the first is the function name or anonymous function, and the second is an iterable object, such as a list. The filer() function is similar to the map() function. They are calculated from the interable object and passed into the function in turn. The difference is that the filter() function will judge the bool value of the result of each execution, and only The bool value is True to filter out. In python2, it still returns a list, in python3 it returns a filter object. You can use the list() method and loop to get the value inside. as follows:

>>> list(filter(lambda x: x/2, [1,2,3,4,5]))

[1, 2, 3, 4, 5]

>>> list(filter(lambda x: x%2, [1,2,3,4,5]))

[1, 3, 5]

>>>

四、sorted

The calling method of the sorted() function is as follows:

sorted(iterable, key=None, reverse=False)

The parameter descriptions are as follows:
interable is an iterable object, such as a list, tuple, etc.;
key, is mainly an element used for comparison, is just a parameter, the specific parameter is taken from the iterable object and specifies one of the iterable objects Element to sort;
reverse, sorting rule, reverse=True means descending order, reverse=False means ascending order, the default is False; the
following example:

>>> sorted([1,9,8,2,3])

[1, 2, 3, 8, 9]

>>> sorted([1,9,8,2,3], reverse=True)

[9, 8, 3, 2, 1]

>>> sorted(L, key=lambda x:x[1])

[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]

>>>

Five, summary

The benefits of functional programming are as follows:
(1) The code is more concise;
(2) There is no loop body in the code, a lot of temporary variables are missing, and the logic is simpler;
(3) Data sets, operations and return values ​​are all Put together

Guess you like

Origin blog.51cto.com/15080014/2654770