Discuss / Python / 第三题

第三题

Topic source
from functools import reducedef str2list(s):    a = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': '.'}    return a[s]def add1(x, y):    return x*10 + ydef str2float(s):    L1 = []    L2 = []    t = map(str2list, s)    a = list(t)    n = a.index('.')    L1[:] = a[:n]    L2[:] = a[n+1:]    k = len(a) - len(L1) - 1    return reduce(add1, L1) + reduce(add1, L2)/pow(10, k)print(str2float('123.456'))

  • 1

Reply