Discuss / Python / 笨办法,好复杂

笨办法,好复杂

Topic source

def str2float(s):

    num = s.index('.')

    front = s[:num] #提取前面的字符串   

    behind = s[num+1:] #提取后面的字符换

    def str2num(i):

        a = {'1':1,'2':2,'3':3,'4':4,'5':5,'6':6, '7':7, '8':8,'9':9,'0':0}

        return a[i]

    front = map(str2num,front) #转化为数字

    behind = map(str2num,behind)

    front = reduce(lambda x,y: x*10+y, front)

    behind = reduce(lambda x,y: x*10+y, behind)

    behind = behind/10**(num) #需要添加小数点

    all = front + behind

    return(all)


  • 1

Reply