PYTHON-LAMBDA-expression

Source: Internet
Author: User

Tag: Lam class number length defines sum expression span TE

Lambda is used to define a function

Common functions:

def multiply (x, y):     return x * y

Use lambda to override the above functions:

Lambda x, y:x * y

The lambda function consists of three parts:

1. Lambda keyword

2. The parameter of the partition is the parameter in the normal function, followed by a :

3. Function body, which is the function body in normal function

Some chestnuts:

# calculates the average of the individual lists in the numbers list, and then returns them to a list numbers = [[The] " ["]. >63, Panax Notoginseng,],[, each, each, each, the list (map (lambda x:sum (x)/len (x), numbers))
Results: [57.0, 58.2, 50.6, 27.2]

 #   filter out cities with a city name less than 10  cities = [ " new York city  " ,  " los Angeles  " ,  " chicago  " ,  mountain View  Span style= "COLOR: #800000" > ", "  denver   ", "   Boston   " ]short_cities  = List ( Filter (lambda  x:len (x) < ten, cities) 
Result: ['Chicago'Denver'Boston ' ]

PYTHON-LAMBDA-expression

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.