Discuss / Python / 不太简洁的try

不太简洁的try

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 char2num(s_):
        return DIGITS[s_]
    def str2int(s_):
        return reduce(lambda x, y: x * 10 + y, map(char2num, s_))

    int_ = []
    postdot_ = []
    flag = 0    n = 0    for i in s:
        if (i != '.') & (flag == 0):
            int_.append(i)
        elif flag == 1:
            postdot_.append(i)
            n = n + 1        else:
            flag = 1            continue    return str2int(int_) + str2int(postdot_) / (10 ** n)

# 第一次运行出错是在这个地方

int_ = postdot_ = []

这样他们会共用同一个地址


  • 1

Reply