Discuss / Python / 第三题

第三题

Topic source

houbo111

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

-- coding:utf-8 --

from functools import reduce def str2float(s): def char2num(t): return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,}[t] if s.find('.')!=-1: s1,s2=s.split('.') if s2=='0': return float(reduce(lambda x,y:10x + y, map(char2num,s1))) else: return reduce(lambda x,y:10x + y, map(char2num,s1+s2))/(10*len(s2)) else: return float(reduce(lambda x,y:10x + y, map(char2num,s))) print('str2float(\'123.456\') =', str2float('123'))


  • 1

Reply