This article tells you how to use the map, filter, reduce three built-in functions in Python, and how to optimize it.
Map () function
The map () function maps the specified sequence according to the provided function.
Syntax: map (function,iterable, ...)
Parameters: Function--functions
Iterable--One or more objects that can be iterated over
Return value: Python2 returns the list, Python3 returns the iterator
Example:
>>>def Square (x): # Calculates the square number ... return x * * 2 ... >>> map (square, [1,2,3,4,5]) # Calculates the square of each element of the list [1, 4, 9, 25]>>> map (Lambda x:x * * 2, [1, 2, 3, 4, 5] # using Lambda anonymous functions [1, 4, 9, 16, 25] # provides two lists to add the list data for the same location >>> map (lambda x, y:x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19]
filter () function
The filter () function is used to filter the sequence, filter out elements that do not meet the criteria, and return a new list of eligible elements.
The two parameters are received, the first is a function, the second is a sequence, each element of the sequence is passed as a parameter to the function, and then returns True or False, and finally the element that returns true is placed in the new list.
Syntax: filter (function, iterable)
Parameters: Function--judging functions
Iterable--Can iterate objects
Return Value: Return list
Example:
def is_odd (n): return n% 2 = = 1 NewList = filter (is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, ten]) print (newlist)
reduce () function
The reduce () function accumulates elements in the parameter sequence.
The function uses all the data in a data collection (linked list, tuple, and so on) to perform the following operations on the function function (with two parameters) passed to reduce, which first operates on the 1th and 2 elements of the set, and the resulting results are then calculated with the third data using the functions, and finally a result is obtained.
Syntax: Reduce (function, Iterable[,initializer])
Parameters: Function--functions
Iterable--Can iterate objects
Initializer--optional, initialization parameters
Return value: Returns the result of the calculation
Example:
>>>def Add (x, y): # Two numbers added ... return x + y ... >>> reduce (add, [1,2,3,4,5]) # calculation list and:1+2+3+4+515>>> reduce (lambda x, Y:x+y, [x, Y, 4,5] # using lambda anonymous function 15
The above understanding the use of three functions, now only start to focus, in the Python3 better substitute:
Functional languages typically provide map, filter, and reduce three higher-order functions (sometimes with different names). In Python 3, map and filter are built-in functions, but they become less important because of the introduction of list derivation and generator expressions. A list deduction or builder expression has the function of map and filter two functions, and is easier to read
>>> def fact (n): # factorial function ... " Returns n! "" ... Return 1 if n < 2 else n * Factorial (n-1)
>>> List (Map (fact, Range (6))) ? [ 1, 1, 2, 6, 120]>>> [Fact (n) for n in range (6)] ? [ 1, 1, 2, 6, 120]>>> list (map (factorial, filter (lambda-N,% 2, range (6))) ? [ 1, 6, 120]>>> [factorial (n) for n in range (6) if n% 2] ? [ 1, 6, 120]>>>
? Build 0! to 5! A list of factorial.
? Use list deduction to perform the same operation.
? Use map and filter to calculate until 5! List of odd factorial.
? Use list derivation to do the same work, replace the map and filter, and avoid using lambda expressions.
Optimization of reduce:
>>> from functools import reduce ?>>> from operator import add?>>> reduce (add, range (100) ) ?4950>>> sum (range) ?4950>>>
? From Python 3.0, reduce is no longer a built-in function.
? Import add to avoid creating a function that specifically asks for the sum of two numbers.
? Calculate the sum of 0~99.
? Use sum to do the same sums, without importing or creating a summation function.
Optimized use of Python map filter reduce