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)
我为啥发表不了评论
对不起,我撤回
Sign in to make a reply
此岸心理咨询
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'))