The most convenient function lambda, never read the god of code!

The most convenient function lambda, never read the god of code!

lambda function is how to use it? Used in what position in what areas can achieve the effect we want to use it? So convenient function there has been talk about you know what? Here we take a look.

Brief introduction

lambda function called anonymous function, with the difference that he is generally a function, do not charge the name of God to think of a function, this is the biggest difference between the two! Therefore, under certain circumstances, particularly convenient to use.

lambda expression:

lambda arg1, arg2, … : expression
The most convenient function lambda, never read the god of code!

The most convenient function lambda, never read the god of code!

Using lambda keyword defined above;
parameters arg1, arg2, etc. represents a list of one or more parameters, the brackets do not need to pay attention;
colon: indicates a new block of statements is not;
the expression: expression is a parameter list parameter calculated
Note: no retrun return, the entire expression is an anonymous function return value;
anonymous function can only be written on one line, also known as one-way function; anonymous function need not be present if while for other statements;

lambda of scenarios:

The lambda function assigned to a variable;

The most convenient function lambda, never read the god of code!

The most convenient function lambda, never read the god of code!

Define a multiplication function, and the value to the variable MUL, can perform mul (1,2);

The lambda functions assigned to other functions;

The most convenient function lambda, never read the god of code!

The most convenient function lambda, never read the god of code!

The most convenient function lambda, never read the god of code!

The standard library sleep time functions as shield, as long as the program is called when the initial law;
the time.sleep the lambda = X: None, sleep time library function does not perform the original function;

The other functions as a lambda function return value;

The most convenient function lambda, never read the god of code!
The most convenient function lambda, never read the god of code!

The lambda functions as arguments passed to other functions (map, filter, reduce);

The most convenient function lambda, never read the god of code!
The most convenient function lambda, never read the god of code!

See two complex Liezi:

With the new sequence after mapping function map output sequence processing.

The most convenient function lambda, never read the god of code!

The most convenient function lambda, never read the god of code!

Using the filter function of the filter outputs odd 0-9;

The most convenient function lambda, never read the god of code!

The most convenient function lambda, never read the god of code!

Guess you like

Origin blog.csdn.net/weixin_41334453/article/details/93481752
Recommended