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.