Discuss / Python / 不知道str.split()用法之前

不知道str.split()用法之前

Topic source

陈蔺川

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

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

    def str2int(s):

        def fn(x, y):

            return x * 10 + y

        def char2num(s):

            return DIGITS[s]

        return reduce(fn, map(char2num, s)) #以上引用教程手动创建int()函数

    m=len(s)

    n=0

    while s[n]!='.':#计算整数有多少位

        n=n+1

    a=str2int(s[0:n])

    b=str2int(s[n+1:m])

    return a+b/(10**(m-n-1))


  • 1

Reply