from functools import reduce def str2float(s): L1 = [] L2 = [] i = 0 while s[i] != '.': L1.append(s[i]) i=i+1 i=i+1 while i<len(s): L2.append(s[i]) i=i+1 x = reduce(lambda x,y:x10+y,map(int,L1)) y = reduce(lambda x,y:x10+y,map(int,L2))/10**len(L2) return x+y
print('str2float(\'123.456\') =', str2float('123.456'))
Sign in to make a reply
0泡老醋
from functools import reduce def str2float(s): L1 = [] L2 = [] i = 0 while s[i] != '.': L1.append(s[i]) i=i+1 i=i+1 while i<len(s): L2.append(s[i]) i=i+1 x = reduce(lambda x,y:x10+y,map(int,L1)) y = reduce(lambda x,y:x10+y,map(int,L2))/10**len(L2) return x+y
print('str2float(\'123.456\') =', str2float('123.456'))