python map() ,reduce()运用
2016-08-18 21:44
197 查看
python3 中map(),reduce()函数
Python内建了map()和reduce()函数。
如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clusters”,你就能大概明白map/reduce的概念。
我们先看map。map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。
举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个
list [1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map()实现如下:
from functools import reduce def f(x): return x * x r = map(f, [1, 2, 3, 4]) print(list(r)) print(r)
map()传入的第一个参数是f,即函数对象本身。由于结果r是一个Iterator,Iterator是惰性序列,因此通过list()函数让它把整个序列都计算出来并返回一个list。
你可能会想,不需要map()函数,写一个循环,也可以计算出结果:
L = [] for n in [1, 2, 3, 4]: L.append(f(n)) print(L)
把这个list所有数字转为字符串,只需要一行代码。
L2 = list(map(str, [1, 2, 3, 4, 5])) print(L2)
reduce把一个函数作用在一个序列[x1, x2, x3, …]上,这个函数必须接收两个参数,
reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
如,对一个序列求和
def add(x, y): return x + y # from functools import reduce d = reduce(add, [1, 2, 3, 4]) print(d)
当然求和运算可以直接用Python内建函数sum(),没必要动用reduce。
但是如果要把序列[1, 3, 5, 7, 9]变换成整数13579,reduce就可以派上用场:
def fn(x, y): return x * 10 + y print(reduce(fn, [1, 3, 5, 7, 9]))
上面这个函数的功能等同于 int()
如果考虑到字符串str也是一个序列,对上面的例子稍加改动,配合map(),我们就可以写出把str转换为int的函数:
def str_to_int(s): def fn_2(x, y): return x * 10 + y def char_to_num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] return reduce(fn_2, map(char_to_num, s)) x = str_to_int('45789') print(x) # 字符 '45789'转换为int 数字
如下为三题其中的简单作业题
1.把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。
输入:[‘adam’, ‘LISA’, ‘barT’],输出:[‘Adam’, ‘Lisa’, ‘Bart’]:
def normalize(name): def charLower(s): return s.lower() def char_to_str(a, b): return a + b reduce(char_to_str, map(charLower, name)) return reduce(char_to_str, map(charLower, name)) def normalize_nameList(L): #处理字符串list return list(map(normalize, L)) L = ['adam', 'LISA', 'barT'] #输入 print(normalize_nameList(L)) #输出['adam', 'lisa', 'bart']
2.Python提供的sum()函数可以接受一个list并求和
请编写一个prod()函数,可以接受一个list并利用reduce()求积:
def prod(L): def chengfa(x,y): return x*y return reduce(chengfa,L) print(prod([1,2,3])) # 输出6
3.利用map
和reduce
编写一个str2float
函数,把字符串'123.456'
转换成浮点数123.456
:
#方法一 def str2float(s): num = s.split('.') return reduce(lambda x, y: x * 10 + y, map(char2num, num[0])) + reduce(lambda x, y: x * 10 + y, map(char2num, num[1]))/(10 ** len(num[1])) #方法二 def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
相关文章推荐
- python map和reduce的用法
- python map/reduce framework
- python的几个内建函数:apply(),filter(),map(),reduce()
- python几个内置函数之-filter,map,reduce
- python map和reduce的用法
- python内置函数map/reduce/filter
- Python特殊语法:filter、map、reduce、lambda [转]
- Map and Reduce (in python)
- python实现用hadoop的map/reduce对web日志进行统计
- python中的map,filter,reduce,lambda (转)
- python的几个内建函数:apply(),filter(),map(),reduce()
- Python 扯淡的Map-Reduce
- Python 扯淡的Map-Reduce
- Python特殊语法:filter、map、reduce、lambda [转]
- python 中的filter, map, reduce方法解释
- python的几个内建函数:apply(),filter(),map(),reduce()
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python的几个内建函数:apply,filter,map,reduce
- Python特殊语法:filter、map、reduce、lambda
- python中的map,filter,reduce,lambda (转)