Python Function

enumerate(iterable[, start]) --> iterator for index, value of iterable

Return an enumerate object. iterable must be another object(a sequence, an iterator, or some other object) that supports iteration. The enumerate object yields pairs containing a count (from start, which defaults to zero) and a value yielded by the iterable argument. enumerate is useful for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...

Equivalent to:

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1

Example:

>>> a = [1, 3, 5, 7, 9]
>>> for index, value in enumerate(a, 1):
            print(index, value)

>>> 1   1
    2   3
    3   5
    4   7
    5   9

zip(iter1 [,iter2 [...]]) --> zip object

Return a zip object whose .__next__() method returns a tuple where the i-th element comes from the i-th iterable argument.  The .__next__() method continues until the shortest iterable in the argument sequence is exhausted and then it raises StopIteration.With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n). This repeats the same iterator n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.

zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use itertools.zip_longest() instead.

Equivalent to:

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)

 Example:

>>> a = [1, 2, 3]
>>> b = [4, 5 ,6, 7]
>>> c = [8, 9]
>>> for x, y, z in zip(a, b, c)
            prin(x, y, z)

>>> 1  4  8
    2  5  9

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2) # return True

map(func, *iterables) --> map object

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().

Example:

>>> int_1 = [1, 2, 3, 4, 5]
>>> int_2 = [6, 7, 8, 9, 10]
>>> ','.join(map(str, int_1))  # return '1,2,3,4,5'
>>> sum(map(operator.mul, int_1, int_2))  #  return 130

map(function, sequence[, sequence, ...]) -> list  ---->Python2
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).

Example:

>>> map(None, [1, 3, 5], [2, 4, 6])  #  return [(1, 2), (3, 4), (5, 6)]
>>> map(None, 'ace123', 'bdf')  # return [('a', 'b'), ('c', 'd'), ('e', 'f'), ('1', None), ('2', None), ('3', None)]

filter(function or None, iterable) --> filter object

Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.

See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.

Example:

>>> L = [1, 2, 3, 4, 5, 6, 7]
>>> list(filter(lambda x: x>4, L))  # return [5, ,6 ,7]

以上函数实现方法:

__getattribute__ __iter__ __new__ __next__ __reduce__

原文地址:https://www.cnblogs.com/yl153/p/6816275.html