Discuss / Python / 使用前几课所学内容书写,代码很长

使用前几课所学内容书写,代码很长

Topic source

dosenkim

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

from functools import reduce

Floatpositon 函数先确定小数点"."的位置

def Floatposition(s): n = 0 for i in s: n = n + 1 if i == ".": break return n

relist 函数去掉小数点后,重新list化。

def relist(s):
L = [] L = list(s)[:Floatposition(s)-1]+ list(s)[Floatposition(s):]

# list中选区位置引用上面的函数
return L

fn 函数把 relist的数值相乘

def fn(x, y):
return int(x)*10 + int(y)

Oxn 函数计算小数点后面的位数,可以认为是10的几次方。

def Oxn(s): i = len(s) - Floatposition(s) m = 1 for n in range(i): m = m * 10

return m

def str2float(s): return float(reduce(fn,relist(s))/Oxn(s))


  • 1

Reply