Discuss / Python / 没有比我这更笨的方法了吧= =

没有比我这更笨的方法了吧= =

Topic source
from functools import reduce
digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s):
    a = []
    b = 0
    c = 0
    index = 0
    def re(s):
        return digits[s]
    for i, n in enumerate(s):

        if n == '.':
            index = i
            break

    a = s[:index]
    print(a)
    b = reduce(lambda x, y: x * 10 + y, map(re, a))
    a = s[index+1:]
    a = a[::-1]
    print(a)
    c = 0.1* reduce(lambda x, y: x * 0.1 + y, map(re, a))
    d = b + c
    return d


  • 1

Reply