Python anonymous functions and recursion

Source: Internet
Author: User
Tags iterable

Lambda

Anonymous functions: A sentence function designed to solve some simple needs

A lambda function does not need a def to declare, a word can declare a function

Grammar:

function name = Lambda parameter: return value

It is important to note that:

1. Functions can have more than one argument, separated by commas

2. Anonymous functions no matter how complex, can only write one line, cut logic to return directly after the data

2. The return value, like a normal function, can be any data type

Anonymous function does not say that there must be no name, the preceding variable is a function name, said he is anonymous because we look at the time by __name__ is not a name, unified called Lambda, in the call is not anything special, like a normal function call.

Sorted ()

Sort function

Syntax: Sorted (iterable, key = None, reverse = False)

Iterable: An Iterative object

Key: A collation (sort function) in which each element of an sorted object is passed to the parameter of the function. Sort by the result of the function operation

Reverse: Whether it is a flashback. True: Flashback, False: Positive sequence

Combined with a function

Combined with Lambda

Filter ()

Filter functions:

Syntax: Filter (function. iterable)

 function : functions used for filtering, in the filter will be automatic bar iterable elements in the pass to function, and then according to function returns TRUE or FALSE to determine whether to preserve this data

  iterable: an Iterative object

Map ()

Mapping functions:

Syntax: Map (function,iterable) can map each element in an iterative object, taking an execution function

Calculates the square of each element in the list, returning the new list

Rewrite it into lambda.

Calculates the data of the same position in the two list and

Recursion:

Calling the function itself in a function is recursive

Python mechanism: The maximum recursion depth is 998

Recursive applications:

We can use recursive traversal to traverse various tree structures, than my own folder system, which can be used recursively to iterate through all the files in the folder

Two-point Search

Binary search. Each time can eliminate half of the data, the search efficiency is very high, but the limitations are relatively large, must be ordered sequence to be able to use binary search

Requirement: Must be ordered sequence

Python anonymous functions and recursion

Related Article

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.