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化。list中选区位置引用上面的函数

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

fn 函数把 relist的数值相乘

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

Oxn 函数计算小数点后面的位数

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

最后总数除于10的整数倍

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

  • 1

Reply