Discuss / Python / map/reduce

map/reduce

Topic source

INVINCIBLEwyq

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

练习1

def normalize(name): 
    return name.title()

练习2

def prod(L):
    return reduce(lambda x,y:x*y , L)

练习3

def char2num(ch):  
    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    return DIGITS[ch] 

ratio = pow(10, len(s) - s.index('.') - 1)
s = s.replace('.', '')
return reduce(lambda x, y: x * 10 + y, map(char2num, s))/ratio

  • 1

Reply