Discuss / Python / 作业

作业

Topic source

极大似然

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

def str2float(s):

    digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, ".": "."}

    def g1(x):

        return digits[x]

    def g2(a, b):

        if b == ".":

            return a

        return a*10+b

    n = s.index(".")

    return reduce(g2, map(g1, s))/10**n


  • 1

Reply