Python Development Basics DAY10 Built-in functions anonymous function recursion

Source: Internet
Author: User

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)forinres:    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:

1 [1491625]

  

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:

1 500

Python Development Basics DAY10 Built-in functions anonymous function recursion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.