Python anonymous functions and map:
from Random Import the randint DEF FUNC (X): return X. 1 + 2 * IF the __name__ == ' __main__ ' : alist = [the randint (. 1, 100) for I in Range (10 )] Print (alist) # Map second each argument to a function func processing, reservation processing result result Map = (func, alist) # used often as a function of the parameter rule result2 = Map ( the lambda X: * X + 2. 1, alist) # use anonymous function as a parameter Print (List (Result)) Print (List (result2))
Output results:
[16, 27, 51, 2, 20, 8, 98, 56, 65, 72] [33, 55, 103, 5, 41, 17, 197, 113, 131, 145] [33, 55, 103, 5, 41, 17, 197, 113, 131, 145]