Discuss / Python / map/reduce 练习

map/reduce 练习

Topic source

KonoNA7

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

normalize()

def normalize(name):
    return name[:1].upper() + name[1:].lower()

<br /> prod()

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

<br /> str2float()

def str2float(s):
    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    def char2num(c):
        return DIGITS[c]
    def fn(x, y):
        return x * 10 + y
    intNum, floatNum = s.split('.')
    intNum = reduce(fn, map(char2num, intNum))
    floatNum = 10**(-len(floatNum)) * reduce(fn, map(char2num, floatNum))
    return intNum + floatNum

KonoNA7

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

<br />标签怎么发出来就失效了:(

ini_g

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

def str2float(s): i = reduce(lambda x, y: x 10 + y, map(char2num_1, list(filter(lambda x: '.' != x, s)))) return i pow(10, -s.index('.') + 1)


  • 1

Reply