from functools import reduce def str2float(s): dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} f = s.index('.') #找到小数点在第几位,返回值为f g = len(s) - 1 - f #计算出小数点后有几位数 s = s[:f] + s[f+1:] #把'.'从s中去掉 def str_int(x): return dict[x] def add(a,b): return a * 10 + b n = reduce(add,(map(str_int,s))) #和str2int同理 f_n = n / 10 ** (g) #n要比我们要求得值大10**g倍数 return f_n
Sign in to make a reply
石头王国精挑细选的石头