Discuss / Python / 自己写又编写失败,参考了一下下边的朋友的,看一下主要思路

自己写又编写失败,参考了一下下边的朋友的,看一下主要思路

Topic source

lyingaloneiii

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

一个包含.的数字字符串要先转为 整数 再找.再原来字符串中的个数,最后把整数转为小数

from functools import reduce

def str2float(s): def fn(x,y): return x10+y n=s.index('.') s1,s2=list(map(int,s[:n])),list(map(int,s[n+1:])) #int函数可用教程中使用map()函数的方法取代 return reduce(fn,s1)+reduce(fn,s2)/(10*len(s2)) print('str2float(\'123.4567\')=',str2float('123.4567'))


  • 1

Reply