Tag: Ack strong value stack Overflow definition represents container calculation nbsp
anonymous function lambda
anonymous function: Lambda x,y:x+y
The above explanation: X, Y is the function parameter, X+y is the function's return value
The naming convention for anonymous functions, identified by the LAMDBA keyword, and the left side of the colon (:) indicates that the function receives the parameter (a, b), and the right side of the colon (:) represents the function's return value (A+B).
Because LAMDBA does not need to be named when it is created, it is called an anonymous function
Equivalent to a normal function:
| 12 |
deftest(x,y): returnx+y |
Anonymous functions simply do not have function names, mainly with built-in functions
Example 1:
| 12345678910 |
# filter##过滤(将布尔值为True的结果过滤出来)name_l=[{"name":"tom","age":222}, {"name": "alex", "age": 333}, {"name": "jack", "age": 133}, {"name": "sun", "age": 363}, ]# 使用匿名函数res=filter(lambdad:d["age"]>200,name_l)fori inres: print(i) |
The execution results are:
| 123 |
{‘name‘: ‘tom‘, ‘age‘: 222}{‘name‘: ‘alex‘, ‘age‘: 333}{‘name‘: ‘sun‘, ‘age‘: 363} |
Example 2:
#map # Merge Mappings
| 123 |
l=[1,2,3,4,5]res=map(lambdax:x**2,l)#求平列表里的平方值,并以列表的方式呈现print(list(res)) |
The execution results are:
Recursive functions:
Definition: Inside a function, other functions can be called. If a function calls itself internally, the function is a recursive function.
Advantages of Recursive functions: simple, logical and clear. In theory, all recursive functions can be written in a circular way, but the logic of the loop is not as clear as recursion.
Recursive properties:
1. There must be a clear end condition
2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion
3. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls that can cause the stack to overflow. )
Example:
| 123456 |
defprice(n): ifn==1: return100 else: returnprice(n-1)+100#在函数内部调用自身print(price(5)) |
The execution results are:
Python Development Basics DAY10 Built-in functions anonymous function recursion