Self-taught python notes with the little turtle lambda expression, filter and map are updating...

After reading these notes, you can get started with Python.
Watch the video of Little Turtle on Station B, and organize your notes by the way.

Lesson 10 list
Lesson 11 Tuples
Lesson 12 strings
Lesson 13 format format
Lesson 14 sequence
Lesson 15 Functions

Lesson 16 lambda expression filter and map

1. What is a lambda expression?

      I don’t know if any friends have learned Java. Anyone who has learned Java should know what anonymous functions are. In fact, lambda expressions are equivalent to anonymous functions. In fact, it doesn’t matter if you don’t understand it. Simply put, it just simplifies the expression of a function.
      It is suitable for replacing functions that are used only once.

2. Usage of lambda expression

      Let me first talk about what the return value of an expression is
Insert picture description here
      . It is easy to understand by taking a few simple examples.
      Suppose we define a function as follows:

def fun1(x):
    return x + 2 * x
fun1(2)

      You also need to call the function if you want to get the result of the function, but if you use lambda expressions, it doesn't need to be so troublesome.

g = lambda x:x + 2 * x
g(2)

Insert picture description here
      The result of both is the same. Another example:
Insert picture description here
      through these two examples should be very clear its usage.

3. Exercises of lambda expressions

1: Please use lambda expression to transform the following function into an anonymous function

def fun_A(x,y=3):
	return x * y

I directly posted the answer to the operation method:
Insert picture description here
2: Convert the anonymous function below into a normal function

lambda x: x if x % 2 else None

The answer is as follows:
Insert picture description here
3: The effect of using lambda expressions:
       ①When writing some execution scripts in Python, using anonymous functions can save the process of defining functions and make the code simpler.
      ②For some functions that are more abstract and only need to call once or twice when the whole program is intelligent, sometimes it is a headache to give the function a function name. When using anonymous functions, you don't need to consider the naming problem.
      ③Simplify the readability of the code, because ordinary functions often jump to the beginning of the def definition part when reading, the use of anonymous functions can save such steps.

4、filter(function or None,iterator)

The filter() method returns an object.
① When the first parameter is None, convert it into a list and output as follows:

list(filter(None,[0,1,False,True])) 

The result is: [1,True]
That is, if the first parameter is None, the default output is True data.
②When the first parameter is a function:

def add(x):
	return x % 2
temp = range(10)
show = filter(add,temp)
list(show)

The output result is: [1,3,5,7,9]
That is, in range(10):0-9, output x% 2 has the index of the remainder data.
Combined with the learning of lambda expressions, the above code can be improved as follows:

list(filter(lambda(x:x % 2),range(10)))
4、map(function or None,iterator)

map is a mapping, the usage is the same as filter, but the final output result is the data value, not the key value. The
same code:

list(map(lambda(x:x % 2),range(10)))

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/A_Tu_daddy/article/details/105125734