tutorial on using lambda to efficiently manipulate lists in Python _python

Source: Internet
Author: User
Tags iterable string format

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)

Related Article

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.