首页 > 代码库 > Python Function

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.

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

 

 

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().

 

 

 

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.

 

 

以上函数实现方法:

__getattribute__(self, name, /)  __iter__(self, /)  __new__(*args, **kwargs)  __next__(self, /)  __reduce__(...)

 

Python Function