Discuss / Python / 第三题

第三题

Topic source

wake-upYC

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

第三题

from functools import reduce

def str2float(s): def f1(x,y): return x10+y def g1(t): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[t] p1=reduce(f1,map(g1,s[0:s.index('.')])) p2=reduce(f1,map(g1,s[s.index('.')+1:]))/(10*len(s[s.index('.')+1:])) return p1+p2

print('str2float(\'123.456\') =', str2float('123.456'))


  • 1

Reply