LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 脚本编程 >

Python内置函数map、reduce、filter的用法

时间:2013-06-20 21:01来源:wubiaoblog.com/archives/584 编辑:吴飚 点击:
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 se
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).

map函数接受的参数为一个处理sequence的函数,及最少一个或多个sequence。map函数会对参数sequence的每个item执行function(item),
最后将执行结果构成一个list返回。

map函数的运用场景大概包括以下几种:
1、处理一个sequence,如,对sequence中的每个值求平方:
>>> a=[1,2,3,4]
>>> map(lambda x:x*x,a)
[1, 4, 9, 16]

2、处理多个sequence,但参数function也需要支持相应数量的参数,如,对两个sequence中相应item做乘法操作:
>>> a=[1,2,3,4]
>>> b=[5,6,7,8]
>>> map(lambda x,y:x*y,a,b)
[5, 12, 21, 32]
>>> map(lambda x:x*x,a,b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (2 given)

3、处理多个sequence时,每个sequence的长度如果不相同,则较短的sequence会被当做在该sequence中增加相应个数(直到达到较sequence元素个数)的None值来处理,如:
>>> a=[1,2,3,4]
>>> b=[5,6,7]
>>> map(lambda x,y:x+y,a,b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
这里我们尝试对a、b这两个list中相应的item做求和操作,但是a有四个元素,而b只有三个,所以b会被挡住为:[5,6,7,None]处理,
而int类型是不能与None类型执行”+”操作的,所以,在执行”4+None”时这里会报错!

4、参数function可以是None,此时分两种情况:
1)、只操作一个sequence,返回一个由sequence中的item组成的list:
>>> a=[1,2,3,4]
>>> map(None,a)
[1, 2, 3, 4]

2)、操作多个sequence,返回一个把有tuple组成的list,tuple包含每个sequence中相应的item:
>>> a=[1,2,3,4]
>>> b=[5,6,7]
>>> map(None,a,b)
[(1, 5), (2, 6), (3, 7), (4, None)]
注意此处,b的第四个元素是被当做为None处理的。

reduce函数的用法
查看下帮助:
>>> 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
    ((((1+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.
reduce函数接受的参数为一个处理sequence的函数,及一个sequence,和一个可选的初始值intial。
参数function接受两个参数。
reduce函数为sequence中item从左到右顺序迭代执行function(item)。

reduce函数的使用非常简单,运用场景大概可以就分为两种:
1、不带初始值,如,对sequence中的item求和:
>>> a=[1,2,3,4]
>>> reduce(lambda x,y:x+y,a)
10

2、带初始值,如,对sequence中的item求和:
>>> a=[1,2,3,4]
>>> reduce(lambda x,y:x+y,a,10)
20
如果有传递initial参数,此处为10,则initial参数会被首先执行。

需要注意的地方是,参数function,必须接受两个参数:
>>> a=[1,2,3,4]
>>> reduce(lambda x:x,a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (2 given)
>>> reduce(lambda x,y,z:x+y+z,a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 3 arguments (2 given)

filter函数的用法
查看下帮助:
>>> help(filter)
Help on built-in function filter in module __builtin__:

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.

filter函数接受的参数为一个sequence和一个处理sequence的function(可以是None)。
filter函数对sequence中的每个item依次执行function(item),将执行结果为true的item依据sequence的类型返回一个list、truple或者string。
filter函数的使用非常简单,大概可以参考以下几种场景:

1、传递一个sequence,列出sequence中的偶数,返回一个list,如:
>>> a=[1,2,3,4,5,6,7,8,9,10]
>>> filter(lambda x:x%2==0,a)
[2, 4, 6, 8, 10]
>>>

2、传递一个sequence,列出sequence中的偶数,返回一个tuple,如:
>>> a=(1,2,3,4,5,6,7,8,9,10)
>>> filter(lambda x:x%2==0,a)
(2, 4, 6, 8, 10)
>>>

3、传递一个sequence,列出sequence中不包含‘a’的其他字符,返回一个string,如:
>>> a='abcdef'
>>> filter(lambda x:x!='a',a)
'bcdef'
>>>

4、如果参数function为None,则对sequence原样返回,感觉这个用法挺没意思的:
>>> a='abcdef'
>>> filter(None,a)
'abcdef'
>>>
补充一下,sequence主要包括以下几种类型:
str
bytes
bytearray
list
tuple
range

实现一个小需求:用map返回乘方列表,reduce计算乘积,filter挑出奇数
#列表a=[1,3,5,7,8,9,4]
a=[1,3,5,7,8,9,4]

#1、对列表a,用map返回乘方列表
mapa=map(lambda x:x*x,a)
print mapa

#2、对列表a,用reduce计算乘积
reducea=reduce(lambda x,y:x*y,a)
print reducea

#3、对列表a,用filter挑出奇数
filtera=filter(lambda x:x%2==1,a)
print filtera
"""

打印出来的结果为:
[1, 9, 25, 49, 64, 81, 16]
30240
[1, 3, 5, 7, 9]
"""

转载请保留固定链接: https://linuxeye.com/program/1776.html

------分隔线----------------------------
标签:Pythonreducefiltermap
栏目列表
推荐内容