#利用map和reduce编写一个str2float函数,把'字符串'转换成'浮点数'
Topic sourced = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'.'} def str2float(s): def offset(s): return pow(10,-s[::-1].index('.')) def f(x,y): if y=='.':return x return x乘10+y def char2num(s): return d[s] return reduce(f,map(char2num,s)) 乘 offset(s)
- 1
_WangZ--
参考评论大神: d = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'.'} def str2float(s): def offset(s): return pow(10,-s[::-1].index('.')) def f(x,y): if y == '.': return x return x10 + y def char2num(s): return d[s] return reduce(f,map(char2num,s))offset(s)