Discuss / Python / 貌似可以直接用```float()```来转换,但还是做了一下

貌似可以直接用```float()```来转换,但还是做了一下

Topic source

顾正遥

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

我的代码:

import functools
def str2float(s):
    a = s.split('.')
    inte = a[0]
    flt = a[1]
    def char2num(x):
        return {'1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '0':0}[x]

    return functools.reduce(lambda x,y: x * 10 + y,map(char2num,inte)) + 0.1*functools.reduce(lambda i,j: i*0.1 + j, map(char2num,flt[::-1]))

print(str2float('123.456'))

  • 1

Reply