char2num这里可以使用ord(char) - 48
Topic source第三题我的解法,还是使用上面例子中的str2int方法
from functools import reduce
import math
def fn(x,y):
return x*10 + y
def char2num(s):
return ord(s) - 48
def str2int(s):
return reduce(lambda x,y: x*10 + y, map(lambda c:ord(c) - 48,s))
def str2float(s):
L = s.split('.')
return reduce(lambda s1,s2: str2int(s1) + str2int(s2) / math.pow(10,len(s2)), L)
print('str2float(\'123.456\') =', str2float('123.456'))
- 1
lubang03
比例子中更为简单,而且更方便改为lamda表达式