Some interesting functions of Python

Source: Internet
Author: User

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

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.