Python's filter, map, reduce, and zip built-in sequence functions

Source: Internet
Author: User
Tags iterable zip in python

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

Noun interpretation: The iterable in this article refers to an iterative object, including sequence and iterator, and other container with an iterative nature.

1. Filter (function, 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 that returns a value to a function, and if true, stores the current element in a string, tuple, or list and eventually returns an object of the corresponding type, or, if False, filters the current element if the function does not return a value. is considered to return none, so all elements will be filtered out. If the first argument is not a function but none, then all elements in Iterable are returned to true.

# function has return value
>>> filter (lambda d:d!= ' a ', ' ABCD ') # filters out the letter ' a '.
' BCD '
    
>>> def d (x): # When you do not use a lambda, you first define a function that acts as a filter

. Return
 True if x!= ' a ' else False
    
>>> filter (d, ' abcd ')
' BCD '
# function has no return value
>>> def f (x):
    if x!= ' a ':
        print x
    else: pass
    
>>> foo = All characters in filter (F, ' ABCD ') # ' ABCD ' are filtered out
b
C
d
>>> foo
'
# The first parameter is None
>>> filter (None, ' ABCD ')     # does not filter anything
' ABCD '
    
  >>> filter (None, [ 1,2,3,4,5,0])   # filtered out 0
  [1, 2, 3, 4, 5]

Note: Only if iterable is a string or tuple, filter returns a string or tuple, and all the rest is returned to list. A function is a single parameter.

2. Map (function, iterable, ...)

The map iterates the elements in the Iterable object into the function and returns a list object to store all the results of the operation. The map supports multiple iterable parameters, and if the number of iterable parameter elements is different, for the lesser number of Iterable objects, the value of the exceeded partial values is None. When the first argument does not have a function of none, a list of tuple is returned, and the elements in each tuple come from each Iterable object.

>>> map (Lambda a:a+1, [1,2,3,4])
[2, 3, 4, 5]
>>> map (Lambda A, b:a+b, [1,2,3,4], (2,3,4,5)) 
  [3, 5, 7, 9]
>>> map (lambda A, b:a + b if b else A +, [1,2,3,4,5], (2,3,4,5))   # 2nd Iterable Object Less

an element
[3, 5, 7, 9]
>>> map (none, [1,2,3,4,5], [1,2,3])
[(1, 1), (2, 2), (3, 3), (4, none), (5, none)]

Note: The number of parameters in the function is the same as the number of iterable parameters in the map.

3. Reduce (function, iterable, start_value)

The reduce function takes the first two values in the Iterable object into the function, and then the operation returns the value as the first argument, and then the third value in the Iterable object is passed into the function to perform the operation, and so on. Until all elements in the Iterable object are evaluated. If the Start_value value is set, then Start_value will act as one of the arguments for the first execution of the function functions and take the first element in iterable as another parameter of the function.

>>> reduce (lambda x, Y:x+y, Range (0,10))
45
>>> reduce (lambda x, Y:x+y, Range (0,10), 10)
55

Note: The function is a two-parameter, and if there is no return value, the none is returned to the next operation.

>>> Reduce (d, range (0,10))
None2
None3
None4
None5
None6 None7 None8 None9

4. zip (seq[, seq, ...])

The ZIP function takes the value of the same index for each sequence object to a tuple, and then returns a list of these tuple. The number of tuple is determined by the sequence with the least index in all sequence, and the sequence is no longer going to continue to value the remaining sequence after the value has been taken.

>>> 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])]

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.