# -*- coding: utf-8 -*- from functools import reduce def str2float(s): m=lambda x,y:x*10+y def mmp(k): A={'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'jd'}[k] return A L=list(map(mmp,s)) return reduce(m,L[:L.index('jd')])+reduce(m,L[L.index('jd')+1:])/10**len(L[L.index('jd')+1:]) print('str2float(\'123.456\') =', str2float('123.456'))
看了参考答案,其实我之前也是想的只用一个reduce然后用全局变量来做的,但是感觉好复杂,还不如直接分成两个来做
Sign in to make a reply
浴火化影