Discuss / Python / 练习打卡-Python教程-函数式编程-高阶函数-map/reduce-第三题(做出来了,输出有异常)

练习打卡-Python教程-函数式编程-高阶函数-map/reduce-第三题(做出来了,输出有异常)

Topic source

南丁叔叔

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

from functools import reduce

str_f = {

    '0': 0,

    '1': 1,

    '2': 2,

    '3': 3,

    '4': 4,

    '5': 5,

    '6': 6,

    '7': 7,

    '8': 8,

    '9': 9,

    '.': -1

}

def str2float(s):

 num = list(map(lambda x:str_f[x],s))

 def list2float(x,y):

  if x == -1:

   x = 0

  if num.index(y)<num.index(-1):

   return x*10+y

  elif num.index(y)==num.index(-1):

   return x

  else:

   i = num.index(y)-num.index(-1)

   return y*10**-i+x

 return reduce(list2float,num)


  • 1

Reply