Python built-in functions

Source: Internet
Author: User
Tags iterable

Map,filter, reduce, zip are built-in methods for sequence data types in Python.

First, Map

Map (func, *iterables)

Take a look at the map function, receive two parameters, the first is a function, the second is a sequence (an iterative object), the role of map is to pass each element of the sequence into the function, and add the result to the new list.

Cases:

Using the map () function, the nonstandard English name entered by the user becomes the first letter capitalized, and the other lowercase canonical names. Input: [' Adam ', ' Lisa ', ' Bart '], output: [' Adam ', ' Lisa ', ' Bart '].

def trans (x):    return X.capitalize () print (list (map (trans, [' Adam ', ' Lisa ', ' Bart ')]) # [' Adam ', ' Lisa ', ' Bart ']

You can also use anonymous functions to get the same result:

Print (list (map (lambda x:x.capitalize (), [' Adam ', ' Lisa ', ' Bart ')]) # [' Adam ', ' Lisa ', ' Bart ']

Of course, without a map, it's no problem using the list to push the formula:

Print ([X.capitalize () for x in [' Adam ', ' Lisa ', ' Bart ']]) # [' Adam ', ' Lisa ', ' Bart ']
Second, reduce

Reduce (function, sequence)

Reduce functions a function in a sequence [X1, x2, x3 ...] , the function must receive two parameters, and reduce calculates the result and the next element of the sequence, with the effect of this:

Reduce (f, [X1, x2, X3, X4]) <--> F (f (f (x1, x2), x3), x4)

Note: In Python3, reduce needs to be imported

From Functools import reduce

Example 1:

Adds 3 to each element in a list, and then sums the elements in the list.

The simplest way to do this is with Sum and list derivation:

Li = [1, 2, 3, 4, 5, 6]print (sum ([x + 3 for x in Li])
# 39

Or:

Li = [1, 2, 3, 4, 5, 6]print (Reduce (lambda x, y:x + y, [x + 3 for x in Li]) # 39

Example 2:

The sum () function provided by Python can accept a list and sum, write a prod () function that accepts a list and uses the reduce () to calculate the product.

def prod (li):    return reduce (lambda x, y:x * y, li) print (prod ([1,2,3,4])    # 24

Or:

Print (Reduce (lambda x, y:x * y, [1, 2, 3, 4])   # 24
Three, zip

Zip (iter1 [, Iter2 [...]])

The zip function takes the same indexed value for each sequence object into a tuple, and then returns a list of these tuple elements. The number of tuples is determined by the sequence with the least index in all sequence, and the sequence will no longer continue to value the remaining sequence after the value is completed.

>>> zip ([1,2,3,4],[2,3,4,5]) [(1, 2), (2, 3), (3, 4), (4, 5)]>>> zip ([1,2,3,4], (2,3,4,5,6,7)) [(1, 2), (2 , 3), (3, 4), (4, 5)]>>> zip ([1,2,3,4], [[2,3,4],[2,3]]) [(1, [2, 3, 4]), (2, [2, 3])]
Iv. Filter

Filter (function or None, iterable)

Filter works by using the first parameter object (function or none) to iterable the second parameter object and filtering the elements in the iterable based on the Boolean value of the result of the operation.

A Boolean operation on the function return value that, if true, stores the current element in a string, tuple, or list and eventually returns an object of the corresponding type, or false to filter out the current element if the function has no return value. is considered to return none, so all elements will be filtered out. If the first parameter is not a function but none, then all elements in iterable that are true are returned.

>>> filter (lambda d:d! = ' A ', ' ABCD ') # filters out the letter ' a '. ' BCD '

  

-----------------------------------------------------------------------------------------------------------

Python built-in 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.