第三题
Topic source怎么拷贝过来都会少了东西的。。。以下是正确的。。。
from functools import reduce
def str2float(s):
s1, s2 = s.split('.')
def char2float(keys):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,}[keys]
def fn(x,y):
return x * 10 + y
return reduce(fn, map(char2float,s1 + s2 )) / 10 ** len(s2)
print('str2float(\'123.456\') =', str2float('123.456'))
- 1
难阿难难难
from functools import reduce
def str2float(s): s1, s2 = s.split('.') def char2float(keys): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,}[keys] def fn(x,y): return x 10 + y
return reduce(fn, map(char2float,s1 + s2 )) / 10 * len(s2)
print('str2float(\'123.456\') =', str2float('123.456'))