Introduced
Lambda
Python is used to support an operator that assigns a function to a variable by default is returned, so no return keyword is added, or it will be an error.
result = Lambda x:x * x result
(2) # return 4
map ()/filter ()/reduce ()
Requires two parameters, the first is a handler, and the second is a sequence (list,tuple,dict)
map ()
Returns a new list of elements in a sequence after they are processed by a handler function
filter ()
Filter the elements in a sequence back to a new list after filtering through functions
reduce ()
Returns a result of an element in a sequence by processing a two-dollar function
Combine the top three functions with a lambda
Li = [1, 2, 3, 4, 5]
# The sequence of each element plus 1
map (Lambda x:x+1, Li) # [2,3,4,5,6]
# returns even filter in the sequence
(lambda x:x% 2 = 0, li) # [2, 4]
# Returns the result of multiplying all elements
reduce (lambda x, y:x * y, li) # 1*2*3*4*5 = 120
Sorted () Sort the list with a lambda
Sorted is used for sorting lists, which is more intelligent than a list with two lists, a dictionary in each list ([{},{}]) requires that two such lists be merged and sorted by time, and that the time in two lists has been converted from time format to string format in order to be able to output through JSON. Field name is Sort_time now put them in reverse order.
the use of sorted
Sorted (iterable, Cmp=none, Key=none, Reverse=false)--> new sorted list terable: is an iterative type; CMP: A function For comparison, comparing what is determined by the key, having a default value, and an item in the iteration set; Key: Use a property and function of a list element as a keyword, have a default value, an item in an iteration set; Reverse: Sorting rules. Reverse = True or reverse = False, with default values. * Return value: is a sorted iteration type, as iterable.
sorted () combining lambda to sort_time sort of iteration type
Sorted (data, Key=lambda d:d[' Sort_time '), reverse=true)