Discuss / Python / 交作业

交作业

Topic source

#我这个思路比较奇葩,利用了上面str2int的方法,在转换list的过程中把‘.’删除,输出整数在除以10*n,然后加入了判断是否为浮点数,不是的话就输出整数。

def str2float(s): def char2float(s): def fn(a): dic = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': '.'} b = dic[a] return b pre_L = list(map(fn, s)) if '.' in pre_L: pre_L.remove('.') now_L = pre_L[:] else: now_L = pre_L[:] def str2num(x, y): return 10 x + y last = reduce(str2num, now_L) return last if '.' in s: p = s.index('.') c = len(s) - p -1 e = char2float(s) return e / (10*c) else: return char2float(s)


  • 1

Reply