Lambda expression for Python3

Source: Internet
Author: User

1. anonymous function

The general cock-silk function is defined like this

def DS (x):     return 2*x+1

And the anonymous function is like this.

Lambda x:2*x+1

Benefits of using Anonymous functions

1, can make the function more concise

2, no need to consider naming, do not have the name of the child racked up ha ha ha haha

3, simplify the code, improve the readability of the Code

2, two commonly used built-in functions 1) filter (parameter 1, parameter 2) parameter 2 with in Parameter 1 is calculated if the true final return output is true for the collection

Use lambda expressions and filter () to find a multiple of 3 within 100

List (filter (lambdanot (n%3), range (1, 100)))

Use the list deduction to replace the problem

 for inch if  not (i%3)]
2, Map (parameter 1, parameter 2) parameter two generation into parameter 1 calculates the collection of the returned calculated value

Analog zip () packages two sequences into elements as list form

>>> list (map (lambda x, y: [x, Y], [1, 3, 5, 7, 9], [2, 4, 6, 8,])) [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

Lambda expression for Python3

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.