Discuss / Python / 做作业

做作业

Topic source
from functools import reduce
def str2float(s):
    def char2num(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
    return reduce(lambda x, y: x * 10 + y, map(char2num, list(filter(lambda s:s!='.',s)))) / 10**(len(s)- s.index('.')-1)

print('str2float(\'123.456\') =', str2float('123.456'))

  • 1

Reply