Anonymous functions
Python allows Lamb da expressions to create anonymous functions.
\
The lambda statement builds a function object and returns a function object, which, if used, requires only a simple assignment. There are no DEF procedures, and you don’t have to worry about naming functions, because this is anonymous. Return a function object with no name. We can use it by assigning any name. After use, the garbage cleaner of the memory, when not in use, automatically remove the garbage, do not consider the problem of releasing resources.
\
Second, the important role of lambda expressions
1. First of all, when Python writes some execution scripts, Python often works with Linux servers to write scripts. Using lambda can save defining functions. Using lambda makes the code much more streamlined.
2. For some relatively abstract functions that only need to be called once or twice during the execution of the whole program, sometimes naming the function is also a headache. Lambda does not need to consider naming problems.
3. Simplify code readability. Since normal functions often skip to the beginning of the def definition, lambda functions can eliminate this step.
\
Two practical BIfs
These two built-in functions are not implemented directly and are used in conjunction with lambda expressions.
1. Filter () : filter
Filter out the resources you need.
How do Python filters implement filtering? Take a look at the notes:
\
\
Filter takes two arguments. The first argument can be a function, a function, or None object. The second argument can be an iterable, an iterable. If the first argument is a function, each element in the second iterable is evaluated as an argument to the function, filtering the values that return true into a list. If the first argument is None, the value of true in the second argument is filtered out.
\
\
Use filter to write the filter that filters out odd numbers.
\
Using lambda expressions:
\
2. Map () : indicates the mapping
The map() function takes two built-in arguments, a function and an iterable sequence.
The function of this BIF is to process each element of the sequence as a parameter of the function, until each element of the iterable sequence is processed, and return the new sequence of all the processed elements.
\
The first argument is a function that becomes a one-sentence function using lambda expressions. Map runs this function along with each iterable element of the sequence that follows it, puts each generated element into the parameter of the function, processes it, gets the processed value, and returns a sequence. Generate 10 numbers from 0 to 9 and multiply by 2 to get 0, 2, 4, 6, 8, 10,12,14,16,18.
\
To consider
1. Pack in flexible lists rather than meta-genies ([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]), can you do that? (Using map and lambda expressions)
\
\
2. Using ZIP binds two tuples together.
\
\
\
This article is shared from the wechat public account – qinghanTester. If there is infringement, please contact [email protected] to delete. This article participates in “OSC source innovation Program”, welcome you to join us and share with us.