Discuss / Python / 第三题写到这里写不下去了,

第三题写到这里写不下去了,

Topic source

了此本心

#1 Created at ... [Delete] [Delete and Lock User]
dic = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9, '.':0.0}    

def f(x,y):
    if(isinstance(y,float)):
        return float(x)
    elif(isinstance(x,float)):
        return x + y/10 #存在问题行
    else:        
        return x*10 +y
def char2num(s):
    return dic[s]

from functools import reduce
def str2float(s):    
    return reduce(f,map(char2num,s))

print(str2float('155.24'))

看了下楼下的,看来必须要 在s给出时find('.')才好确定后面的位数。


  • 1

Reply