Discuss / Python / 为啥没格式

为啥没格式

Topic source

欧石楠

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

n = s.index('.')

    s = s.split('.')

    s = s[0] + s[1]

    def fn(x,y):

        return x*10+y

    def strInt(s):

        d = {'1':1,'2':2,'3':3,'4':4,'5':5,'6':6}

        return d[s]

    intNum = reduce(fn,map(strInt,s))

    i = 1

    while n > 0:

        i *= 10

        n -= 1

    return intNum / i

欧石楠

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

不好意思,上面的有bug,修改如下:

from functools import reducedef str_float(s):    n = s[::-1].index('.')    s = s.split('.')    s = s[0] + s[1]    def fn(x,y):        return x*10+y    def strInt(s):        d = {'1':1,'2':2,'3':3,'4':4,'5':5,'6':6}        return d[s]    intNum = reduce(fn,map(strInt,s))    i = 1    while n > 0:        i *= 10        n -= 1    return intNum / iprint(str_float('1.234'))print(str_float('12.34'))print(str_float('123.4'))

  • 1

Reply