from functools import reduce
def Floatposition(s): n = 0 for i in s: n = n + 1 if i == ".": break return n
def relist(s): L = [] L = list(s)[:Floatposition(s)-1]+ list(s)[Floatposition(s):]
# list中选区位置引用上面的函数 return L
def fn(x, y): return int(x)*10 + int(y)
def Oxn(s): i = len(s) - Floatposition(s) m = 1 for n in range(i): m = m * 10
return m
def str2float(s): return float(reduce(fn,relist(s))/Oxn(s))
Sign in to make a reply
dosenkim
from functools import reduce
Floatpositon 函数先确定小数点"."的位置
def Floatposition(s): n = 0 for i in s: n = n + 1 if i == ".": break return n
relist 函数去掉小数点后,重新list化。
def relist(s):
L = [] L = list(s)[:Floatposition(s)-1]+ list(s)[Floatposition(s):]
fn 函数把 relist的数值相乘
def fn(x, y):
return int(x)*10 + int(y)
Oxn 函数计算小数点后面的位数,可以认为是10的几次方。
def Oxn(s): i = len(s) - Floatposition(s) m = 1 for n in range(i): m = m * 10
def str2float(s): return float(reduce(fn,relist(s))/Oxn(s))