Discuss / Python / 套用老师的str2int,把float放大为int转换,最后记得除10的幂次转换回去。。。

套用老师的str2int,把float放大为int转换,最后记得除10的幂次转换回去。。。

Topic source

子鱼若鱼

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

[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 fn(x, y):
    return x*10+y
def char2num(s):
    return DIGITS[s]
n = len(s)-1-s.find('.')
s = s.replace('.','')
return reduce(fn, map(char2num, s))/10**n]

  • 1

Reply