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