A tutorial on using lambda to efficiently manipulate lists in Python

Source: Internet
Author: User
Tags iterable
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)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.