from functools import reduce def str2float(s): zheng = s.split('.')[0] fen = list('0' + s.split('.')[1]) fen.reverse() 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] zheng_float = reduce(lambda x, y: x 10. + y, map(char2num, zheng)) fen_float = reduce(lambda x, y: x 0.1 + y, map(char2num, fen)) print zheng_float + fen_float
print str2float('2343242.2342')
Sign in to make a reply
木夕舛
from functools import reduce def str2float(s): zheng = s.split('.')[0] fen = list('0' + s.split('.')[1]) fen.reverse() 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] zheng_float = reduce(lambda x, y: x 10. + y, map(char2num, zheng)) fen_float = reduce(lambda x, y: x 0.1 + y, map(char2num, fen)) print zheng_float + fen_float
print str2float('2343242.2342')