规范名字 求积 字符串转换成浮点数
Topic sourcefrom functools import reduce
def str2float(s):
dot = 0
sign = 1
if s[0] == '-': # 如果s有负号
s = s[1:] # 记下负号后所有字符,s的长度减1
sign = -1 # # 负号记为-1
for i in range(len(s)):
if s[i] == '.': # 如果s的第i位是小数点
s = s[:i] + s[i + 1:] # 删去小数点字符,s的长度减1
dot = len(s) - i # 记录小数点位数,小数点唯一
break # 运行一次后就跳出循环,因为s的长度本身发生变化
def fn(x, y):
return x * 10 + y
def char2num(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
return reduce(fn, map(char2num, s)) / (10 ** dot) * sign
x1 = '123456789'
x2 = '123456.789'
x3 = '-123.456789'
print('str2float(\'', x1, '\') =', str2float(x1)) # str2float(' 123456789 ') = 123456789.0
print('str2float(\'', x2, '\') =', str2float(x2)) # str2float(' 123456.789 ') = 123456.789
print('str2float(\'', x3, '\') =', str2float(x3)) # str2float(' -123.456789 ') = -123.456789
加上符号判断。lambda方法不太会 我的作业地址,欢迎交流指正 https://github.com/xiajava/learn-python3
- 1
数据学家琪露诺
1 规范名字
2 求积
3 字符串转换成浮点数