Discuss / Python / map/reduce

map/reduce

Topic source

七月上行

#1 Created at ... [Delete] [Delete and Lock User]

map()传入的第一个参数是f,即函数对象本身。由于结果r是一个Iterator,Iterator是惰性序列,因此通过list()函数让它把整个序列都计算出来并返回一个list。

def normalize(name): return name[0].upper() + name[1:].lower() # str不可迭代遍历但是可以索引

def prod(L): def product(x, y): return x*y return reduce(product, L) 第一题

def normalize(name): return name[0].upper() + name[1:].lower() 第二题

def prod(L): def product(x, y): return x*y return reduce(product, L) 第三题 i=s.find('.') #找到小数点 s1, s2 = list(map(int, s[:i])), list(map(int, s[i+1:])) r1, r2 = reduce(lambda x,y:10*x+y, s1), reduce(lambda x,y:0.1*x+y, s2[::-1])*0.1 return r1+r2


  • 1

Reply