Detailed Explanation of python lambda

  • 2021-09-12 01:34:44
  • OfStack

Directory 1. lambda Syntax 2. lambda Use Simple Example 3. 4 Uses of lambda

1. lambda syntax

lambda argument_list: expression

lambda is a reserved keyword for Python, and argument_list (parameter list) and expression (expressions about parameters) are user-defined.

2. Simple example of lambda use

lambda x, y: x*y; The function inputs are x and y, and the output is their product x*y lambda: None; The function has no input parameters and the output is None lambda * args: sum (args); The input is an arbitrary number of parameters, and the output is their sum (implicit requirement is that the input parameters must be able to perform addition operations) lambda **kwargs: 1; The input is an arbitrary key-value pair parameter and the output is 1

3. Four Uses of lambda

Assign the lambda function to a variable through which the lambda function is invoked indirectly

add = lambda x, y:x+y
add(1,2)
>>>3
Assign the lambda function to other functions, so that other functions are replaced by the lambda function. (I don't know what this means yet.)

For example, to mask the functionality of the function sleep in the standard library time (Mock), we can call: time. sleep = lambda x: None at program initialization. In this way, calling the sleep function of the time library in subsequent code will not perform the original function. For example, when time. sleep (3) is executed, the program does not sleep for 3 seconds, but does nothing.

Return the lambda function to the caller as the return value of other functions

The return value of a function can also be a function. For example, return lambda x, y: x+y returns one addition function. In this case, the lambda function is actually a function defined within a function, called a nested function, or an internal function. Correspondingly, functions containing nested functions are called external functions. The ability of internal functions to access local variables of external functions is the basis of closure (Closure) programming.

Pass the lambda function as an argument to other functions

Some Python built-in functions receive functions as parameters. Typical built-in functions of this kind have these.

(1) filter function. At this time, the lambda function is used to specify the conditions for filtering list elements. For example, filter (lambda x: x% 3 = = 0, [1, 2, 3]) specifies that the elements in the list [1, 2, 3] that can be divisible by 3 are filtered out, and the result is [3].

(2) sorted function. In this case, the lambda function is used to specify the criteria for sorting all elements in the list. For example, sorted ([1, 2, 3, 4, 5, 6, 7, 8, 9], key=lambda x: abs (5-x) sorts the list [1, 2, 3, 4, 5, 6, 7, 8, 9] according to the distance between elements and 5, and the result is [5, 4, 6, 3, 7, 2, 8, 1, 9].

(3) map function. In this case, the lambda function is used to specify a common operation on every 1 element in the list. For example, map (lambda x: x+1, [1, 2, 3]) adds 1 to each of the elements in the list [1, 2, 3], resulting in [2, 3, 4].

(4) reduce function. In this case, the lambda function is used to specify the combination conditions of pairwise adjacent elements in the list. For example, reduce (lambda a, b: '{}, {}'. format (a, b), [1, 2, 3, 4, 5, 6, 7, 8, 9]) combines the elements in the list [1, 2, 3, 4, 5, 5, 6, 7, 8, 9] in pairs from left to right in the form of comma-separated characters, resulting in '1, 2, 3, 4, 5, 6, 7, 8, 9'.

In addition, some Python library functions also receive functions as parameters, such as the spawn function of gevent. At this point, the lambda function can also be passed in as an argument.

The above is the python lambda use details, more information about the use of python lambda please pay attention to other related articles on this site!


Related articles: