Discuss / Python / 交作业,写的不对的请指点

交作业,写的不对的请指点

Topic source
# -*- coding: utf-8 -*-
#字符型转为浮点型
from functools import reduce
def char2num(s):
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':10}[s]

def str2float(s):
    L = list(map(char2num,s))
    for x in L:
        if x - 10 == 0:
            n = L.index(x)
            l = L[-(len(L)-n)+1:]
            return reduce(lambda x,y:x*10+y,L[:n]) + (reduce(lambda x,y:(x*0.1)+y,l[::-1]))*0.1
print (str2float('123.456'))

  • 1

Reply