python怎么判断字符串可以转换浮点数
1、raw_input()默认接受的是字符串,而后边需要的数字,所以就需要进行类型转换。

2、rom functools import reduce
def str2float(s):
def f(s):
return {'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

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

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

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:69
阅读量:101
阅读量:68
阅读量:43
阅读量:173