Python commonly used built-in functions to introduce "filter,map,reduce,apply,zip"

Source: Internet
Author: User
Tags stdin


Python is a very concise, elegant language, many of its built-in functions combined, you can use a small number of code to achieve a lot of complex functions, if the same function to let C/c++/java to implement, it may be a big head, in fact, Python is a complex data structure hidden in the built-in functions , in C, so just write your own business logic Python will automatically come up with the results you want. The built-in functions in this area are mainly, filter,map,reduce,apply, combined with anonymous functions, list parsing together, the function is more powerful. The most obvious benefits of using built-in functions are:



1. fast, using built-in functions, than the ordinary Python implementation, faster than about one times. Speed equivalent to C + +



2. Code Simplicity



Buildin function Source Link address: (interested can see ^_^)



Https://hg.python.org/cpython/file/57c157be847f/Python/bltinmodule.c



Https://docs.python.org/3.3/library/functions.html



Filter:



Grammar:


>>> Help (Filter) Help on
built-in function filter in module __builtin__:

filter (...)
    Filter (function or None, sequence)-> list, tuple, or string return

    those the items of sequence for which function (item ) is true.
    the If function is None and return the items that are true.  If sequence is a tuple
    or string, return the same type, else return a list.


Use:



Used to filter values that do not match the function func (), similar to the Select Value!= ' a ' in SQL



Equivalent to an iterator that invokes a Boolean function Func to iterate over each element in Seq, returning a sequence that is Bool_seq return true >>> first argument: function or none, functions or none



>>> second parameter: sequence, sequence



Description



>>> If the first argument is a function, then the sequence that returns the condition is true (list, meta ancestor or string)



>>> returns all items in the sequence that are true if the first argument is None



Example 1: To filter out all lists with a value of false





Print filter (none,[-2,0,2, ', {}, ()])     #output [ -2,2], all the rest is false


Example 2: Filter a letter


>>> filter (lambda x:x!= ' a ', ' ABCD ')
' BCD '
Example 3: Filter the names of the letters starting with B
>>> names = [' Alice ', ' Bob ', ' Smith ', ' David ', ' Barbana ']
>>> filter (lambda x:x.startswith (' B '), names)
[' Bob ', ' Barbana ']
Example 4: Filter the odd number in the Fib list, even items
>>> fib = [0,1,1,2,3,5,8,13,21]
>>> filter (lambda x:x%2,fib)   #Is actually equivalent to x%2 = 1 items filtered
[1, 1, 3, 5,,
>>> filter (lambda x:x%2 ==0,fib)
[0, 2, 8]
Example 5: Bring out all 2-20 of the mass sequences
>>> filter (lambda x:not [x for I in range (2,x) If x%i = 0],range (2,20))
[2, 3, 5, 7, 11, 13, 17, 19]
Example 6: Filter names for empty sequences
>>> names = [' Alice ', ' Jerry ', ' Sherry ', ' Bob ', ' Tom ', ']
>>> filter (none,names)
[' Alice ', ' Jerry ', ' Sherry ', ' Bob ', ' Tom '

Example 7: Filter all files ending with test.py in a directory

import os, re #required modules
files = os.listdir (r'D: \ python ') #List all files in the directory you want to find
test = re.compile ('test.py $', re.IGNORECASE) # re.IGNORECASE ignore case
print filter (test.search, files) #Filter all files that meet the conditions

>>>
['1test.py', 'test.py']
Example 8: Filter all sublists where the word is 'Python'
def filter_word (word):
  try:
    return word! = 'Python'
  except ValueError:
    return False

words = [['Perl', 'Python', 'Shell'], ['Java', 'C / C ++'], ['VB', 'Dephi']]

print [filter (filter_word, word) for word in words]
#Used list comprehension
The logical implementation of filter:

def filter (func, seq):
    f_seq = [] #Build an empty sequence for storing filtered elements
    for item in seq: #Iterate over each element in the sequence
        if func (item): #if true
            f_seq.append (item) #If the conditions are met, then join
    return f_seq #Return filtered elements

print filter (lambda x: x> 0, [-2,0, 2]) #Filter anonymous functions and return positive values
>>>
[2]
map:
>>> help (map)
Help on built-in function map in module __builtin__:

map (...)
    map (function, sequence [, sequence, ...])-> list

    Return a list of the results of applying the function to the items of
    the argument sequence (s). If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length. If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).
use:
>>> Perform the same operation on one or more sequences and return a list

Explanation:

1. Return a list, the list is the result set of parameters func on seq1, seq2

2. There can be multiple sequences, if the function is None, return a list of sequences

Example 1: general usage

>>> map (lambda x: x + 1, [1,2,3,4])
[2, 3, 4, 5]
>>> map (lambda x, y: x + y, [1,2,3,4], (10,20,30,40))
[11, 22, 33, 44]
>>> map (lambda x, y: x + y if y else x + 10, [1,2,3,4,5], (1,2,3,4))
#The fifth element in the first sequence exists, but does not exist in the second sequence, so y is False, so execute 5 + 10
[2, 4, 6, 8, 14]
>>> map (None, [1,2,3,4,5], (1,2)) #If None, use None to fill in the gap caused by the short sequence
[(1, 1), (2, 2), (3, None), (4, None), (5, None)]
>>> names = ['Alice', 'Jerry', 'Bob', 'Barbar']
>>> map (len, names) #Find the length of each element in the list
[5, 5, 3, 6]
>>> m = [1,4,7]
>>> n = [2,5,8]
>>> map (None, m, n)
[(1, 2), (4, 5), (7, 8)]
>>> import operator #Compare element sizes in two lists
>>> a = [1,2,3]
>>> b = [0,4,9]
>>> map (operator.gt, a, b)
[True, False, False]
Example 2: Find the number between 0-5, [self, square, cube], such as: element 2, then return: [2,4,8]
def func1 (x): return x #Return to itself
def func2 (x): return x ** 2 #return square
def func3 (x): return x ** 3 #return cube

funcs = [func1, func2, func3] #function list

for i in range (5): #traverse the list
    print map (lambda func: func (i), funcs)
Example 3: Implement the following logical structure
1.0 [1,2,3,4,5]

2.0 [1,2,3,4,5]

....

foos = [1.0,2.0,3.0,4.0,5.0]
bars = [1,2,3,4,5]


def test (foo):
    print foo, bars


print map (test, foos)
Example 4: Combine map and filter, find the square of even numbers between 0-10, and combine them into a list
>>> map (lambda x: x ** 2, filter (lambda x: x% 2 == 0, range (10)))
[0, 4, 16, 36, 64]
Example 5: Use map function to implement the following business logic
s = [50,62,15,76,57,97,82,99,45,23]
'' '
Require: print the grade from s as belows:
grage <60-E
grade <70-D
grade <80-C
grade <90-B
grage> 90-A
'' '

s = [50,62,15,76,57,97,82,99,45,23]

def grage (x):
    try:
        if x <60:
            return 'E'
        else:
            if x <70:
                return 'D'
            elif x <80:
                return 'C'
            elif x <90:
                return 'B'
            else:
                return 'A'
    except ValueError:
        print 'Input error, x should be int!'

li = map (lambda x: "{0}-{1}". format (x, grage (x)), s) #format the output

for i in li: #traverse the generated list
    print i

>>>
50-E
62-D
15-E
76-C
57-E
97-A
82-B
99-A
45-E
23-E
The logical implementation of map:

def map (func, seq):
    map_seq = [] #Build empty sequence
    for item in seq: #Process each element in the sequence
        map_seq.append (func (item)) #Add func processed elements to the empty sequence
    return map_seq #Return to the last list

print map (lambda x: x * 2, [1,2,3,4]) # [2,4,6,8]
reduce:
use:

func is a binary function that applies func to the elements of the seq sequence, each carrying a pair (the previous result and the elements of the next sequence), and continuously applies the existing result and the next value to the subsequent result obtained In the end, our sequence is reduced to a single return value: if the initial value init is given, the first comparison will be init and the first sequence element instead of the first two elements of the sequence.

Explanation:

1. Functools module must be imported in Python 3.0, from functools import reduce

2. Reduce must return a value, which can have an initial value.

>>> help (reduce)
Help on built-in function reduce in module __builtin__:

reduce (...)
    reduce (function, sequence [, initial])-> value

    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce (lambda x, y: x + y, [1, 2, 3, 4, 5]) calculates
    ((((11 + 2) +3) +4) +5). If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
example:

>>> reduce (lambda x, y: x + y, [47,11,42,13])
113
The implementation process is shown in the following figure:

Introduction to common built-in functions in Python [filter, map, reduce, apply, zip]
The implementation process is equivalent to the following:

>>> import operator
>>> reduce (operator.add, [47,11,42,13])
113
NOTE:
1. func () function cannot be None, otherwise an error is reported

>>> reduce (None, [1,2,3,4])
Traceback (most recent call last):
  File "<stdin>", line 1,
in <module>
TypeError: 'NoneType' object is not callable
2. func (x, y) can only have two parameters, otherwise an error is reported:
>>> reduce (lambda x, y, z: x + y + z, [1,2,3,4], 9)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda> () takes exactly 3 arguments (2 given)
The logical implementation of reduce:
def reduce (func, seq, init = None):
    l_seq = list (seq) #Turn to list first
    if init is None: #if initial value
        res = l_seq.pop (0)
    else:
        res = init

    for item in l_seq:
        res = func (res, item) #func (res, item) is passed to res as a result set

    return res #Return result set
print reduce (lambda x, y: x + y, [1,2,3,4]) #result is 10
print reduce (lambda x, y: x + y, [1,2,3,4], 10) #The result is 20, and the initial value of init is 10
apply:
grammar:

>>> help (apply)
Help on built-in function apply in module __builtin__:

apply (...)
    apply (object [, args [, kwargs]])-> value

    Call a callable object with positional arguments taken from the tuple args,
    and keyword arguments taken from the optional dictionary kwargs.
    Note that classes are callable, as are instances with a __call __ () method

    Deprecated since release 2.3. Instead, use the extended call syntax:
        function (* args, ** keywords).
use:

>>> When the parameters of a function exist in a tuple or a dictionary, it is used to call the function indirectly. The parameters in the tuple or dictionary are passed in order.

Explanation:

1. args is a tuple containing the positional parameters passed in accordance with the parameters required by the function. = 3, b = 4), but not in the order (b = 4, a = 3)
2. kwargs is a dictionary containing keyword arguments, and if args is not passed, kwargs needs to be passed, you must leave empty in the position of args

3. The return value of the apply function is the return value of the func function.

4. In fact, apply (func, args, kwargs) has been replaced by func (* args, ** kwargs) since Python2.3.

Example 1: General use

def func1 (): #No parameter function
  print 'No Args!'

def func2 (arg1, arg2): #Two parameters
  print arg1, arg2

def func3 (arg1 = 1, arg2 = 2): #with dictionary function
  print arg1, arg2

if __name __ == '__ main__':
  apply (func1)
  apply (func2, ('Hello', 'World!'))
  apply (func3, (), ('arg1': 'This is param1', 'arg2': 'This is param2')) #Note that the ancestor parameter is ()
Since func (* args, ** kwargs) can be used instead of apply (). So what are the benefits of apply, and a few visible benefits,

1. If the function name or variable name is too long, it is convenient to use apply ().

2. If you can't confirm how many variables are in args, you must use apply, she can dynamically load variables and functions

# Sorry about the long variable names ;-)

args = function_returning_list_of_numbers ()
func = function_returning_a_function_which_operates_on_a_list_of_numbers ()

# You want to do f (arg [0], arg [1], ...) but you don't know how many
# arguments are in 'args'. For this you have to use 'apply':

result = apply (func, args)
3. If the function name is passed as an object, it is convenient to use apply ()
def test (f, a, b):
    print 'test'
    print f (a, b)

test (func, a, b)
#It can be seen that the first parameter f of the test function is a function object. We pass func to f,
#Then what f () in test actually does is what func () does
#This way, we greatly provide flexibility in the program. If we have another function instead of func, we can use the same test function
test (lambda x, y: x ** 2 + y, 2,3)

>>>
test
7
zip
>>> help (zip)
Help on built-in function zip in module __builtin__:

zip (...)
    zip (seq1 [, seq2 [...]])-> [(seq1 [0], seq2 [0] ...), (...)]

    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences. The returned list is truncated
    in length to the length of the shortest argument sequence.
use:

>>> returns a list of ancestors, which contains the corresponding elements of each sequence in order, whichever is the smallest

Explanation:

>>> This built-in function is actually quite easy to understand. The returned object is a list of ancestors, which is easy to understand by looking at examples.

example:

>>> zip (range (5), range (1,20,2))
[(0, 1), (1, 3), (2, 5), (3, 7), (4, 9)]
>>> x = (1,2,3); y = (4,5); z = (6,)
>>> zip (x, y, z)
[(1, 4, 6)]
The logical implementation of zip:
def zip (* iterables):
    # zip ('ABCD', 'xy')-> Ax By
    sentinel = object ()
    iterators = [iter (it) for it in iterables]
    while iterators:
        result = []
        for it in iterators:
            elem = next (it, sentinel)
            if elem is sentinel:
                return
            result.append (elem)
        yield tuple (result)
See this comprehensive example:
http://www.360doc.com/content/14/0507/11/7821691_375450523.shtml
http://my.oschina.net/cloudcoder/blog/226461

 

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.