from functools import reduce def str2float(s): def f1(x,y): return 10*x+y def f2(x,y): return x/10+y def num(x): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[x] return reduce(f1,list(map(num,s.split('.')[0])))+reduce(f2,list(map(num,s.split('.')[1][::-1])))/10
str1='123.456' print('123.456 -->',str2float(str1))
Sign in to make a reply
sparkipp
from functools import reduce def str2float(s): def f1(x,y): return 10*x+y def f2(x,y): return x/10+y def num(x): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[x] return reduce(f1,list(map(num,s.split('.')[0])))+reduce(f2,list(map(num,s.split('.')[1][::-1])))/10
str1='123.456' print('123.456 -->',str2float(str1))