网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

python怎么判断字符串可以转换浮点数

时间:2024-10-14 12:05:42

1、raw_input()默认接受的是字符串,而后边需要的数字,所以就需要进行类型转换。

python怎么判断字符串可以转换浮点数

2、rom functools import reducedef str2float(s): def f(s): return {&#泌驾台佐39;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

python怎么判断字符串可以转换浮点数

3、pos = s.find('.')

4、 front = reduce(g,map(f,s[0:pos]))

python怎么判断字符串可以转换浮点数

5、 rear = reduce(g,map(f,s[pos+1:]))/10**(len(s)-pos-1)return front+rear

python怎么判断字符串可以转换浮点数

6、print('str2float(\'123.456\') =', str2float('123.456'))

python怎么判断字符串可以转换浮点数
© 2025 阿力知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com