str2float
Topic sourceDIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.':'.'}
fact = [10]
def fn(x, y):
if y == '.':
fact[0] = 1.0
return x
if fact[0] < 2.0:
fact[0] = fact[0] * 0.1
return x + y* fact[0]
else:
return x *10 + y
def char2num(a):
return DIGITS[a]
return reduce(fn, map(char2num, s))
fact用单独变量
- 1
cacaty
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.':'.', 'fact': 10.0}
def fn(x, y):
fact = DIGITS['fact']
if y == '.':
DIGITS['fact'] = 1.0
return x
if fact < 9.0:
fact = fact * 0.1
DIGITS['fact'] = fact
return x + y* fact
else:
return x *10 + y
def char2num(a):
return DIGITS[a]
return reduce(fn, map(char2num, s))