Discuss / Python / 一起学习

一起学习

Topic source

黑岩74032

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

这只支持'xxx.xxx'或'.xxx'的形式

def str2float(s):
    def prod(x,y):
        return x * 10 + y

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

    num = list(map(char2num, s))
    point = num.index('.')
    num.pop(point)
    return reduce(prod, num) / 10**(len(num) - point)

  • 1

Reply