python备忘之内建函数map、filter、reduce

python的内建函数中有三个比较好用的函数,map(), filter() 和 reduce()

版本 Python 2.7.6

map函数

函数声明:map(function, sequence[, sequence, ...]) -> list, tuple, or string

函数说明: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). 

用法示例:

 1 >>> map(lambda x: x**2, [1,2,3,4])    #列表中各元素做平方
 2 [1, 4, 9, 16]
 3 >>> map(lambda x: x+2, (1,2,3,4))    #元组中各元素加2
 4 [3, 4, 5, 6]
 5 >>> map(lambda x: x+'a', "abcd")    #字符串中每个字符加'a'
 6 ['aa', 'ba', 'ca', 'da']
 7 >>> map(lambda x,y: x+y, [1,2,3], [4,5,6])  #两个列表对应元素相加
 8 [5, 7, 9]
 9 >>> map(None, [1,2,3])    #function为None
10 [1, 2, 3]
11 >>> map(None, [1,2,3], 'abcd')    #function为None
12 [(1, 'a'), (2, 'b'), (3, 'c'), (None, 'd')]

关于map的实现,没翻到源码,根据个人理解,map函数的处理过程,以上面第7行中两个列表元素相加为例,其实相当于

1 >>> [sum(item) for item in map(None, [1,2,3], [4,5,6])]    # 先将两列表中的对位元素两两搭配,不足补None
2 [5, 7, 9]

filter函数

函数声明:filter(function or None, sequence) -> list, tuple, or string

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

用法示例:

 1 >>> filter(lambda x : x>3, [1,2,3,4,5,6])    #返回列表中大于3的元素
 2 [4, 5, 6]
 3 >>> filter(lambda x : x in 'abc', 'abcde')    #返回字符串'abcde'中包含在'abc'中的字符组成的串
 4 'abc'
 5 >>> filter(None, [0,1,2,3])    #function为None,返回列表中值为真的元素
 6 [1, 2, 3]
 7 >>> filter(None, 'abc')    #function为None
 8 'abc'
 9 >>> filter(None, [True, False, True])    #返回列表中值为真的元素
10 [True, True]

当function为None时,filter函数相当于 [item for item in sequence if item] 

当function为非None时,filter函数相当于 [item for item in sequence if function(item)] 

reduce函数

函数声明:reduce( fuction, sequence [, initvalue] ) ->  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. 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.
用法示例:
1 >>> reduce(lambda x,y: x+y, [1,2,3,4])    #省略初始值 (((1+2)+3)+4) = 10
2 10
3 >>> reduce(lambda x,y: x+y, [1,2,3,4], 5)     #初值为5,((((5+1)+2)+3)+4) = 15
4 15
对于function必须要接收两个参数,设reduce的返回值为ret
在第一个例子中,过程为:
ret为空,所以 ret = s[0] = 1,然后 for y in s[1:]: ret = function(ret, y)   return ret
在第二个例子中,过程为:
ret有初始值为5,所以ret = 5, 然后 for y in s[:]: ret = function(ret, y)   return ret
下面是翻到的关于reduce函数的源码,可以参考下
 1 // /python/src/Modules/_functoolsmodule.c
 2 /* reduce() *********************************************/
 3 static PyObject * functools_reduce(PyObject *self, PyObject *args)
 4 {
 5     PyObject *seq, *func, *result = NULL, *it;
 6 
 7     if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result))
 8         return NULL;
 9     if (result != NULL)
10         Py_INCREF(result);
11 
12     it = PyObject_GetIter(seq);
13     if (it == NULL) {
14         PyErr_SetString(PyExc_TypeError,
15             "reduce() arg 2 must support iteration");
16         Py_XDECREF(result);
17         return NULL;
18     }
19 
20     if ((args = PyTuple_New(2)) == NULL)
21         goto Fail;
22     
23     for (;;) {
24         PyObject *op2;
25 
26         if (args->ob_refcnt > 1) {
27             Py_DECREF(args);
28             if ((args = PyTuple_New(2)) == NULL)
29                 goto Fail;
30         }
31 
32         op2 = PyIter_Next(it);            //获取列表中的下一个元素
33         if (op2 == NULL) {
34             if (PyErr_Occurred())
35                 goto Fail;
36             break;
37         }
38 
39         if (result == NULL)                //若result为空,即初始值省略,将seq[0]的值赋给result
40             result = op2;
41         else {
42             PyTuple_SetItem(args, 0, result);   // result为func函数的第一个参数
43             PyTuple_SetItem(args, 1, op2);      // 从列表中获取的元素为func函数的第二个参数
44             if ((result = PyEval_CallObject(func, args)) == NULL)
45                 goto Fail;
46         }
47     }
48 
49     Py_DECREF(args);
50 
51     if (result == NULL)
52         PyErr_SetString(PyExc_TypeError,
53                    "reduce() of empty sequence with no initial value");
54 
55     Py_DECREF(it);
56     return result;
57 
58 Fail:
59     Py_XDECREF(args);
60     Py_XDECREF(result);
61     Py_DECREF(it);
62     return NULL;
63 }
View Code

 

posted on 2014-09-22 22:56  FYHO5417  阅读(286)  评论(0)    收藏  举报

导航