http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317852443934a86aa5bb5ea47fbbd5f35282b331335000
利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456:
rom functools import reduce
def str2float(s):
def f(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': '.'}[s]
def g(x, y):
return x*10 + y
pos = s.find('.')
front = reduce(g,map(f,s[0:pos]))
rear = reduce(g,map(f,s[pos+1:]))/10**(len(s)-pos-1)
return front+rear
print('str2float(\'123.456\') =', str2float('123.456'))