from functools import reduce
def str2float(s): def fn(x,y): return x10+y n=s.index('.') s1,s2=list(map(int,s[:n])),list(map(int,s[n+1:])) #int函数可用教程中使用map()函数的方法取代 return reduce(fn,s1)+reduce(fn,s2)/(10*len(s2)) print('str2float(\'123.4567\')=',str2float('123.4567'))
Sign in to make a reply
lyingaloneiii
一个包含.的数字字符串要先转为 整数 再找.再原来字符串中的个数,最后把整数转为小数
from functools import reduce
def str2float(s): def fn(x,y): return x10+y n=s.index('.') s1,s2=list(map(int,s[:n])),list(map(int,s[n+1:])) #int函数可用教程中使用map()函数的方法取代 return reduce(fn,s1)+reduce(fn,s2)/(10*len(s2)) print('str2float(\'123.4567\')=',str2float('123.4567'))