Discuss / Python / work 3

work 3

Topic source

程乔静

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

-- coding: utf-8 --

from functools import reduce

def str2float(s): m=len(s)-s.index('.')-1 digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':None} def char2num(s): return digits[s] def fn(x,y): if y==None: return x else: return x10+y return reduce(fn,map(char2num,s))/10*m

print('str2float(\'123.456\') =', str2float('123.456')) str2float('123.456') = 123.456 if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')

测试成功!

digits函数不熟呀


  • 1

Reply