from functools import reduce def str2float(s): a,b=s.split('.') def K(x,y): return x*10+y def cha2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] A=reduce(K,map(a,cha2num(a))) B=reduce(K,map(b,cha2num(b))) return A+B/len(B) print('str2float(\'123.456\') =', str2float('123.456'))
想知道我的代码哪里有问题额。。。。
return A+B/10**len(B)
不是除以B的长度 而是除以B的长度位数0.234是要除以1000的
Sign in to make a reply
不知所云的推杆
from functools import reduce def str2float(s): a,b=s.split('.') def K(x,y): return x*10+y def cha2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] A=reduce(K,map(a,cha2num(a))) B=reduce(K,map(b,cha2num(b))) return A+B/len(B) print('str2float(\'123.456\') =', str2float('123.456'))
想知道我的代码哪里有问题额。。。。