Discuss / Python / 自己写的

自己写的

Topic source

浴火化影

#1 Created at ... [Delete] [Delete and Lock User]
# -*- 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'))

浴火化影

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

看了参考答案,其实我之前也是想的只用一个reduce然后用全局变量来做的,但是感觉好复杂,还不如直接分成两个来做


  • 1

Reply