A brief introduction to the use of filter and lambda functions in Python _python

Source: Internet
Author: User
Tags in python

Filter (function or None, sequence), where sequence can be list, tuple,string. The function of this function is to filter out all the elements in the sequence.

Filter (function or None, sequence), where sequence can be list, tuple,string. The function of this function is to filter out all elements in the sequence that return TRUE or bool (return value) to true when invoking the function as the element itself, and return with the list. Filter can only accept two parameters (Function,sequence), where only one value can be returned in function functions

First, a simple piece of code:

Print Max (filter (lambda x:555555% x = = 0, range (100, 999))

The code means the output is 555555 of the maximum three digits of the approximate number.

First, the first knowledge point of this code is Python's built-in function filter

The filter () function, which is used to filter the list. The simplest way to say this is to filter a list with a function that passes each item of the list into the filter function, and the filter function returns false to remove the item from the list.

The filter () function includes two parameters, function, and list. That is, the function filters the items in the list parameter based on whether the result returned by a function parameter is true, and then returns a new list.

In simple terms, the filter () function is equivalent to the following code:

c = [B for b in A1 if B > 2]
print C

The second knowledge point is the lambda () function

Python supports this syntax, which allows users to quickly define the smallest functions of a single line, which are called lambda functions borrowed from Lisp.

def f (x): Return
  x * 2
g = Lambda x:x * 2
(Lambda x:x * 2) (3)

As you can see in the code, the lambda function completes the same thing as the ordinary function, and the lambda has no parentheses around the argument list, and the return keyword is ignored (return implicitly exists because the entire function has only one row and the function has no name. But it can be assigned to a variable for invocation)

The last piece of code shows that a lambda function is just an inline function.

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.