Discuss / Python / 只用前面的函数

只用前面的函数

Topic source

H

#1 Created at ... [Delete] [Delete and Lock User]
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'.'}
def str2float(s):
    def char2num(s):
        return DIGITS[s]
    def fn1(x, y):
        return x * 10 + y
    def fn2(x, y):
        return x * 0.1 + y
    num_list = list(map(char2num,s))
    L1 = []
    L2 = []
    for i in range(0,len(num_list)+1):
        if num_list[i] != '.':
            L1.append(num_list[i])
        else:
            break
    for i in range(1,len(num_list)+1):
        if num_list[-i] !=  '.':
            L2.append(num_list[-i])
        else:
            break
    return(reduce(fn1,L1) + 0.1 * reduce(fn2,L2))
    

  • 1

Reply