filter, lambda function expression
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) as True when the function is called with the element itself as a parameter and return it as a list. The filter can only accept two parameters (function, sequence), among which the function Only one value can be returned in the function
Let’s start with a simple code:
print max(filter(lambda x: 555555 % x == 0, range(100, 999)))
The code means to output the divisor of the largest three-digit number of 555555.
First of all, the first knowledge point of this code is python’s built-in function filter
filter() function, which is used to filter lists. The simplest way to put it is to use a function to filter a list, pass each item in the list into the filter function, and delete the item from the list when the filter function returns false.
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 the function parameter is true, and finally returns a new list.
Simply speaking, 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 a single-line minimal function , these functions, called lambdas, are borrowed from Lisp.
def f(x): return x * 2 g = lambda x: x * 2 (lambda x: x * 2)(3)
As you can see from the code, the lambda function completes the same thing as the ordinary function, and the lambda has no brackets around the parameter list and ignores the return keyword (return exists implicitly because the entire function only has one line , and the function has no name, but it can be assigned to a variable and called)
The last piece of code shows that the lambda function is just an inline function.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
