Introduced
Lambda
One of the operators that Python uses to support assigning a function to a variable is returned by default, so you don't have to add the return keyword, or you'll get an error.
result = Lambda x:x * Xresult (2) # return 4map ()/filter ()/reduce ()
Requires two parameters, the first one is a handler function, the second is a sequence (list,tuple,dict)
Map ()
Returns a new list after the elements in the sequence are processed by processing functions
Filter ()
Returns a new list of elements in a sequence after they are filtered through a function
Reduce ()
Returns a result of an element in a sequence that is processed by a two-tuple function
Use the above three functions in conjunction with a lambda
Li = [1, 2, 3, 4, each element in the 5]# sequence plus 1map (lambda x:x+1, Li) # [2,3,4,5,6]# Returns the even filter in the sequence (lambda x:x% = = 2, li) # [0, 4]# return 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 lambda
Sorted used for list sorting, more intelligently than the list comes with two lists, each with a dictionary ([{},{}]) that requires two such lists to be sorted by time, two of the time in the list in order to be able to pass the JSON output has been converted from a time format to a string format. The field is named 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 key, having a default value, iterating over an item in the set; Key: A property and function of a list element as a keyword, with a default value, an item in the iteration set; Reverse: Sorting rules. Reverse = True or reverse = False, with a default value. * Return value: is a sorted, iterative type, as with iterable.
Sorted () combined with lambda to sort an iterative type with Sort_time
Sorted (data, Key=lambda d:d[' Sort_time '), reverse=true)