字符串转浮点数 str2float (python版本)
来源:互联网 发布:java web im 开源 编辑:程序博客网 时间:2024/05/20 17:28
利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456:
# -*- coding: utf-8 -*-from functools import reducedef str2float(s): 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] n = s.index('.') return reduce(lambda x,y:x*10+y,map(char2num,s[:n]+s[n+1:]))/(10**n)
print('str2float(\'123.456\') =', str2float('123.456'))
思路:char2num函数用来将字符串转化为int,然后我们跳过小数点,将123.456变为123456,再用reduce函数 把它们变为整数,最后除以10^n 即可得到对应的小数
0 0
- 字符串转浮点数 str2float (python版本)
- python: 字符串转浮点数
- python 字符串转浮点数
- 利用map和reduce编写实现字符串转化为浮点数的str2float()函数
- 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
- 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
- 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
- python 浮点数字符串转int类型
- 字符串转浮点数
- 字符串转浮点数
- 字符串转浮点数
- Python:大小写转换:normalize,求乘积:prod,字符串转float:str2float
- Python 字符串 转换 整数 浮点数 数字
- python数据类型转换 python字符串转浮点数 python字符串转float
- python map reduce 将浮点字符串转为浮点数
- 字符串转浮点数 字符串转整型数
- str2float
- C中浮点数转字符串
- 人生的意义
- 线性判别分析(2)
- [web面试必备]深入浅出HTTP协议
- 文章标题
- 有效处理Java异常三原则
- 字符串转浮点数 str2float (python版本)
- 计算机中的信息表示
- 欢迎使用CSDN-markdown编辑器
- LAMP环境搭建
- C++继承与派生的异同点
- 10月英语,美好的开始
- 连接池连接数据库
- 省市县三级联动
- linux按行读取一个文件,然后输出