Discuss / Python / 人生苦短,我用Python

人生苦短,我用Python

Topic source

hcie_zpc

#1 Created at ... [Delete] [Delete and Lock User]
    s1 = ''
    while s[:1] != '.':  # 分离整数和小数
        s1 += s[:1]     # 整数 
        s = s[1:]       # 小数
    s = s[1:]      # 去除小数点
    Digits = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
    def fn(x,y):
        return x*10 + y
    def char2num(s):     # 转化小数
        return Digits[s]
    def char2num1(s1):      # 转化整数
        return Digits[s1]

    return reduce(fn,map(char2num1,s1)) + reduce(fn,map(char2num,s)) * pow(0.1,len(s))

  • 1

Reply