# python内置函数-第一篇

### zip

#### zip 函数简介

Python内置zip函数的签名如下：

itertools.izip()的功能类似。 从函数签名可以知道该函数的参数接收一个或多个可以迭代的对象。

1. zip 函数的参数可以是零个或多个可迭代的参数（eg. tuple, list, map）。
2. 当参数是tuple或list类型时，它将每个参数同一个索引位置的元素组成一个tuple，放入结果list的对应位置
3. 如果参数为空，或参数中有一个为空的参数，则返回一个空list
4. 如果参数类型是dict类型，则它将每个dict的key按顺序组成tuple，放入结果list的对应位置

### map

map函数的签名是：

Apply function to every item of iterable and return a list of 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. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

Apply function to every item of iterable and return a list of 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.

If one iterable is shorter than another it is assumed to be extended with None items.

If function is None, the identity function is assumed;

if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object;

the result is always a list.

map函数的返回值总是一个list。

### reduce

reduce函数的签名如下：

reduce函数的官方说明：

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:

reduce 函数的第一个参数function是一个接收两个参数并返回一个计算的函数，该函数对参数iterable中相邻的每两个元素进行连续的计算，最终将生产一个值作为reduce函数的返回值。

### filter

filter函数的签名如下：

Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to [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.ifilter() and itertools.ifilterfalse() for iterator versions of this function, including a variation that filters for elements where the function returns false.

filte函数的作用很简单，针对参数iterable中的每个元素应用函数function，并将返回true的元素放入结果list。

### max & min

max 函数签名：

Return the largest item in an iterable or the largest of two or more arguments.
If one positional argument is provided, iterable must be a non-empty iterable (such as a non-empty string, tuple or list). The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.
The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, max(a,b,c,key=func)).

min 函数签名：

Return the smallest item in an iterable or the smallest of two or more arguments.
If one positional argument is provided, iterable must be a non-empty iterable (such as a non-empty string, tuple or list). The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.
The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func)).

min函数的工作原理和max相同，就不在赘述。