Discuss / Python / 拿走不谢

拿走不谢

Topic source

from functools import reduce

def str2float(s): L = list(map(str, s)) try: dot = L.index('.') except: dot = None print('L=', L) if dot != None: L.pop(dot) print('L=', L) return reduce(lambda x, y: x 10 + y, map(int, L[0:dot])) + \ 0.1 reduce(lambda x, y: x / 10 + y, map(int, L[-1:dot - 1:-1]))

print(str2float('123.456'))

L = list(map(str, s)) 这句其实没啥卵用,s如果是一个多位整数,这行直接报错,s如果是个字符串,完全可以写成L = list(s)

heartbeatnice

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

我为啥发表不了评论

heartbeatnice

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

对不起,我撤回


  • 1

Reply