The for loop is like a Swiss army knife, it can solve many problems, however, when the code you need to glance quickly figure out what the code does, they may make people know what to do.
map, filter and reduce these three techniques may provide an alternative to the iterative function that describes the reason, in order to avoid excessive for loop.
Draw focus: Iteration
What is the Map, Filter and Reduce?
When writing code for 95% of the time is spent traversing the string or array. In this case, typically one of the following: mapping each value to a series of statements, the filter value that satisfies a specific condition, or to reduce the data set to a single aggregate value.
In fact, loop through function normally fall into one of three categories:
-
Map: apply the same set of steps for each item, storing the result
-
Filter: application verification condition evaluates to True storage items
-
Reduce: Returns a value passed from element to element
Map / Filter Python in / Reduce
In Python, as a function of the presence of these three technologies, instead of an array or string class methods. This means, have to write map (function, my_list), instead of writing my_array.map (function).
In addition, each technology requires a transfer function that will execute each project. Typically, this function is as an anonymous function (called arrow head function in JavaScript) written. However, in Python, it is often seen to be using a lambda expression .
The syntax between lambda expressions and arrow functions are actually very similar. The => Replace: Be sure to use keywords and lambda, remaining almost the same.
// Python Lambda Expression
square = lambda number: number * number
One key difference between the arrow and the lambda expression is a function, the function can be extended to a full arrow function by a plurality of statements, the lambda expression is limited to single expression returned. Thus, when using the map (), filter () or the reduce (), you need to perform a plurality of operations for each item, define the function, then it contains.
def inefficientSquare(number):
result = number * number
return result
map(inefficientSquare, my_list)
Replacement for loop
The following are examples of three common for loops, which are map, filter and reduce replacement. Target: squares and calculating the odd list.
First, the basic loop for example. Note: The following code is simply to demonstrate, even if there is no map / filter / reduce is also room for improvement.
numbers = [1,2,3,4,5,6]
odd_numbers = []
squared_odd_numbers = []
total = 0
# filter for odd numbers
for number in numbers:
if number % 2 == 1:
odd_numbers.append(number)
# square all odd numbers
for number in odd_numbers:
squared_odd_numbers.append(number * number)
# calculate total
for number in squared_odd_numbers:
total += number
# calculate average
Converting each of the steps of one of these three functions:
from functools import reduce
numbers = [1,2,3,4,5,6]
odd_numbers = filter(lambda n: n % 2 == 1, numbers)
squared_odd_numbers = map(lambda n: n * n, odd_numbers)
total = reduce(lambda acc, n: acc + n, squared_odd_numbers)
There are several important points to emphasize grammar.
-
map () and filter () available for the machine. However, reduce () must be imported from more than 3 version of Python libraries
-
lambda expression is a first argument of all three functions, iterable second parameter
-
reduce () lambda expression requires two parameters: the accumulator (passed to the value of each element) and a single element itself
via:https://medium.com/better-programming/how-to-replace-your-python-for-loops-with-map-filter-and-reduce-c1b5fa96f43a