from functools import reduce
def str2float(s):
def fn(x, y):
return x * 10 + y
def char2num(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
return reduce(fn, map(char2num, s.replace(".","")))
s="1234.567"
if s.find(".")!=-1:
print('str2float(\'%s\') ='%s, str2float(s)/pow(10,(len(s)-s.find(".")-1)))
else:
print('str2float(\'%s\') ='%s, str2float(s))
利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
最新推荐文章于 2022-06-20 11:23:09 发布