【发布时间】:2018-03-23 09:09:11
【问题描述】:
当我运行我的代码时,它会产生一个KeyError
from functools import reduce
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s):
dot_index=s.index('.')
new_str = s[:dot_index]+s[dot_index:]
print(new_str)
return reduce(lambda x,y:x*10+y,map(lambda x:DIGITS[x],new_str))/(10**(len(new_str)-dot_index))
num = str2float('123.456')
print(num)
【问题讨论】:
标签: python-3.x