Discuss / Python / 第三题换了个思路

第三题换了个思路

Topic source

木夕舛

#1 Created at ... [Delete] [Delete and Lock User]

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')


  • 1

Reply