Lambda function:in a lambda statement, the colon is preceded by a parameter, can have multiple, separated by commas, and the return value to the right of the colon
1. Map/reduce function
(1)map()The function receives two parameters, one is a function, the other is a sequence, the map incoming function functions sequentially to each element of the sequence, and returns the result as a new list
""= map (lambda x:x*x, [1, 2, 3, 4, 5, 6, 7, 8, 9])print lis T
""= map (str, [1, 2, 3, 4, 5, 6, 7, 8, 9])print list
(2) reduce () function receives two parameters, one is a function, one is a sequence, and the elements in the sequence return a result by processing a two-tuple function, reduce The result continues and the next element of the sequence is calculated as a cumulative
"'"= reduce (lambda x, y:x * y, [1, 2, 3, 4, 5])print result
2. Filter function: The function receives two parameters, one is a function, one is a sequence, the elements in the sequence are filtered through the function to return a new list
"'" = Filter (lambda x:x% 2 = = 0, [1, 2, 3, 4, 5, 6, 7, 8, 9]) C13>print List
3. Sorted function: The function can sort the list, it can also receive a comparison function to implement a custom sort
" "to sort a list" "List= Sorted ([1, 2, 3, 4, 5, 6, 7, 8, 9], reverse=False)PrintList" "use a custom function to sort the list in reverse order" "defreversed_cmp (x, y):ifX >y:return-1ifX <y:return1return0list= Sorted ([1, 2, 3, 4, 5, 6, 7, 8, 9], reversed_cmp)PrintList
Python built-in functions