其实一开始我的代码很长.然后就参考了大家的代码,博取众长. 果然是life is short.
from functools import reduce def str2float(s): def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.':None}[s] return reduce(lambda x, y: x * 10 + y, map(char2num,[x for x in s if x != '.']))/(10**(len(s)-s.index('.')-1)) print("'123.456' => ",str2float('123.456')) print("'12.3456' => ",str2float('12.3456'))
Sign in to make a reply
飞霜流云
其实一开始我的代码很长.然后就参考了大家的代码,博取众长. 果然是life is short.