First, anonymous function
What is anonymous is a function:
Functions or subroutines that do not need to use the DEF function name
function Syntax:
Lambda Parameters: Expressions
Function Features:
1. Lamdba is just an expression that eliminates the process of defining a function and makes the code more streamlined
2. Lamdba the logic of encapsulation in an expression is limited
3. Cannot access parameters outside of its own parameter sequence or global namespace
Example:
Second, built-in map () function:
Map (function, collection);
Function: Map receives a function and then acts on each element in the collection and returns the result as a new collection.
Note: Map must have a return value.
For example, add 1 to each element of the array:
Since map returns a collection, it needs to be converted to a list output value:
Three, reduce () function:
Reduce (function, collection)
Function: Reduce accepts a function and then acts on the collection. First, the 12th element in the collection functions, then the resulting result and the third element of the function operation, until the last element.
For example, to accumulate elements of a collection:
Iv. Map/reduce
Reduce (function, map (function, collection))
Function: Map/reduce is used in combination with the map and reduce functions. A map operation is performed on the collection, another collection is returned, and a result is returned by the reduce operation on the collection.
For example: Calculates the sum of the squares after the set is opened:
Iv. Filter function
Filter (function, collection)
Filter filters the sequence, passing the elements of the collection into the function sequentially, returning True if the element returns to remain in the collection, otherwise the element is not preserved
For example, to remove even numbers from a collection:
Output with List:
Because the filter returns an iterative object, it needs to be output individually in the Anaconda, and cannot be turned into a list output. However, the pycharm can be converted to a list output.
———————————————————————————————————————————————————————————
If there is something wrong, I hope you will come forward and thank you very much.
Python Some fun functions