Discuss / Python / 使用if...else...判断带小数点与不带小数点两种情况

使用if...else...判断带小数点与不带小数点两种情况

Topic source
def str2float(str):    if str.find('.') > 0:        str_s = str.split('.')        return reduce(lambda x, y: 10*x+y, map(char2int, str_s[0])) + reduce(lambda x, y: 10*x+y, map(char2int, str_s[1]))/10**len(str_s[1])    else:        return reduce(lambda x, y: 10 * x + y, map(char2int, str))
def char2int(char):    return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[char]def str2float(str):    if str.find('.') > 0:        str_s = str.split('.')        return reduce(lambda x, y: 10*x+y, map(char2int, str_s[0])) + reduce(lambda x, y: 10*x+y, map(char2int, str_s[1]))/10**len(str_s[1])    else:        return reduce(lambda x, y: 10 * x + y, map(char2int, str))

  • 1

Reply