Discuss / Python / 唉,比较一下自己写的好乱

唉,比较一下自己写的好乱

Topic source

Nicktimebreak

#1 Created at ... [Delete] [Delete and Lock User]
在此插入代码

from functools import reduce

def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.':10}[s]

def str2float(s): L = list(map(char2num,s)) length = len(L) n = 0 pos = 1 while n < length: if L[n] == 10: break n = n + 1 if n < length: pos = len(L) - n - 1 L.remove(10) num = reduce(lambda x, y: x 10 + y, L) / (10**pos) else: num = reduce(lambda x, y: x 10 + y, L) return num print('str2float(\'123.456\') =', str2float('1234.56'))


  • 1

Reply