from functools import reduce
str_f = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'.': -1
}
def str2float(s):
num = list(map(lambda x:str_f[x],s))
def list2float(x,y):
if x == -1:
x = 0
if num.index(y)<num.index(-1):
return x*10+y
elif num.index(y)==num.index(-1):
return x
else:
i = num.index(y)-num.index(-1)
return y*10**-i+x
return reduce(list2float,num)
Sign in to make a reply
南丁叔叔
from functools import reduce
str_f = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'.': -1
}
def str2float(s):
num = list(map(lambda x:str_f[x],s))
def list2float(x,y):
if x == -1:
x = 0
if num.index(y)<num.index(-1):
return x*10+y
elif num.index(y)==num.index(-1):
return x
else:
i = num.index(y)-num.index(-1)
return y*10**-i+x
return reduce(list2float,num)