Python is known to support several programming paradigms:Procedural (using basic statements), Object Oriented Programming and Functional Programming.
Python also provides tools for other functional programming languages:
- Tools for calling functions on iterable objects using map
- Use filters to filter items
- A tool that uses reduce to apply a function to pairs of terms to run the result.
I. MAP
Handles iterable objects without loops:map
One of the operations that we sometimes need to do with iterable sequences such as lists, collections, dictionaries, etc. is to perform some kind of operation on each of the element values in them to collect the results.
For example, selecting a column in the database to add or subtract, or squaring for some particular value.
Let's look at an example first:
>>> test = [1, 2, 3, 4, 5, 6] >>> square = [] >>> for x in test: (x*x) >>> square [1, 4, 9, 16, 25, 36] >>>
At this point it is possible to utilize thePython
(used form a nominal expression)map
The technique that allows you to process and transform all items explicitly without using loops is often referred to as mapping. A map can be useful when you need to apply a transformation function to an iterable and convert it to a new iteration.
>>> test = [1, 2, 3, 4, 5, 6] >>> >>> def square(num): return num*num >>> list(map(square, test)) [1, 4, 9, 16, 25, 36] >>>
As above, we'll pass in a custom functionsquare()
to use it more generally, that is, to apply this function to every element in the list.
map is called for each element in the listsquare
function and collects the return values into a new list.
Just because we need to customize a square function, in conjunction with the previous articlelambda
function in a simple introduction. We can generate this anonymous function directly using lambda, i.e., we can write code like this to achieve the same functionality:
>>> list(map((lambda x: x*x), test)) [1, 4, 9, 16, 25, 36] >>>
map passes in built-in Python functions
In addition to custom functions, you can pass in built-in Python functions in map. For example, if you have a list of strings, you can easily create a new list that calculates the length of that list of strings:
>>> name = ["Sam", "Dwen", "Kyrie"] >>> list(map(len, name)) [3, 4, 5] >>>
map Advanced Usage
Not only does a map accomplish the same thing that a for loop does, but it also has performance benefits. map has advanced uses such as: in a sequencable type, map takes one set of parameters after another from each sequence, in order, in parallel, and passes them into the function:
>>> pow(2, 8) 256 >>> pow(3,8) 6561 >>> list(map(pow, [1,2,3], [8, 8, 8])) [1, 256, 6561] >>>
As you can see from the above code, map takes a value for each of the incoming sequences in parallel.
Second, map and list derivatives
The map call is actually similar to the list derivation style.
>>> test = [1, 2, 3, 4, 5, 6] >>> [x*x for x in test] [1, 4, 9, 16, 25, 36] >>> list(map((lambda x: x*x), test)) [1, 4, 9, 16, 25, 36]
But map will in general run faster than a list derivation, and it will take less code to write. And here's an important point: by surrounding a derivation with parentheses instead of square brackets, you can create an object that generates values on demand, reducing both memory and program response time.
III. Selecting elements in an iterable object: filter
The map function is a major and relatively unambiguous representation of Python's functional programming toolset. filter and reduce implement the ability to select elements of an iterable object based on a test function and to apply functions to "pairs" of elements, respectively.
Here's a call to filter to pick out the elements of a sequence that are greater than zero:
>>> list(range(-10, 10)) [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list(filter((lambda x : x > 0), range(-10, 10))) [1, 2, 3, 4, 5, 6, 7, 8, 9]
filter For an element in a sequence or iterable object, if the function returns a True value for that element, the element is added to the result list.
Like map, filter can be equated with a for loop, but filter is built-in, concise, and usually runs faster:
>>> result = [] >>> for x in range(-10, 10): if x > 0: (x) >>> resullt [1, 2, 3, 4, 5, 6, 7, 8, 9]
The same functionality can be achieved with list derivatives:
>>> [x for x in range(-10,10) if x > 0] [1, 2, 3, 4, 5, 6, 7, 8, 9]
Merging elements in an iterable object: reduce
Pythonreduce()
is a function that resides in the Python standard library in a module called functools.
from functools import reduce
Calculates the sum of all the elements in a list by reducing:
>>> reduce((lambda x, y: x + y),[1,2,3,4,5]) 15
reduce
The current and next element in the list is passed into the listed lambda function, and by default, the first element in the sequence initializes the starting value.
This use of reduce also accomplishes the same thing as using a for loop as follows:
>>> test = [1, 2, 3, 4, 5] >>> result = test[0] >>> for x in test[1:]: result = result + x >>> result 15
V. Summary
- map consists of applying a conversion function to an iterable to generate a new iterable. The items in the new iteration are generated by calling the conversion function on each item in the original iteration.
- A filter consists of applying a predicate or boolean function to an iterable to generate a new iterable. Items generated by filtering any item in the original iterable for any item for which the predicate function returns false.
- reduce consists of applying the reduce function to an iteration to produce a single cumulative value.
To this point this article on Python function compilation programming of the three treasures map + filter + reduce share the article is introduced to this, more related to Python map + filter + reduce treasures please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!