from functools import reduce
def str2float(s): import math import re if re.match('^\d+(.\d+)?$',s) == None: print('不是float类型') return sList = str(s).split('.') res1 = reduce(lambda x, y: x * 10 + y, map(int, sList[0])) if len(sList) > 1: res2 = reduce(lambda x, y: x * 10 + y, map(int, sList[1])) return res1 + res2 * math.pow(10, -len(sList[1])) else: return reduce(lambda x, y: x * 10 + y, map(int, s))
Sign in to make a reply
UTOO_NAIVE
from functools import reduce
def str2float(s): import math import re if re.match('^\d+(.\d+)?$',s) == None: print('不是float类型') return sList = str(s).split('.') res1 = reduce(lambda x, y: x * 10 + y, map(int, sList[0])) if len(sList) > 1: res2 = reduce(lambda x, y: x * 10 + y, map(int, sList[1])) return res1 + res2 * math.pow(10, -len(sList[1])) else: return reduce(lambda x, y: x * 10 + y, map(int, s))