Python字符串转换成浮点数函数分享
作者:hebedich 时间:2022-10-04 01:04:37
利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
from functools import reduce
def str2float(s):
return reduce(lambda x,y:x+int2dec(y),map(str2int,s.split('.')))
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]
def str2int(s):
return reduce(lambda x,y:x*10+y,map(char2num,s))
def intLen(i):
return len('%d'%i)
def int2dec(i):
return i/(10**intLen(i))
print(str2float('123.456'))
标签:Python,字符串,浮点数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 通过logging写入日志到文件和控制台的实例
2021-04-26 00:04:38
Web设计色彩速查表
2009-12-21 16:24:00
在PyTorch中使用标签平滑正则化的问题
2021-12-26 17:46:54
![](https://img.aspxhome.com/file/2023/8/99468_0s.png)
Django框架会话技术实例分析【Cookie与Session】
2021-06-24 03:41:53
一文带你吃透Python中的os和sys模块
2022-01-04 20:42:55
![](https://img.aspxhome.com/file/2023/0/76010_0s.png)
解剖JavaScript中的null和undefined
2009-03-01 12:49:00
python抓取网站的图片并下载到本地的方法
2022-05-19 23:45:08
python基础之多态
2022-04-05 10:06:06
![](https://img.aspxhome.com/file/2023/2/107462_0s.png)
为SWFUpload增加ASP版本的上传处理程序
2011-04-28 08:01:00
python实现读取excel表格详解方法
2023-03-12 04:48:37
![](https://img.aspxhome.com/file/2023/9/70719_0s.png)
浅谈Keras中fit()和fit_generator()的区别及其参数的坑
2022-04-18 07:22:26
![](https://img.aspxhome.com/file/2023/3/88903_0s.jpg)
Django restful framework生成API文档过程详解
2021-08-10 16:37:16
![](https://img.aspxhome.com/file/2023/5/76005_0s.png)
Sql Server 和 Access 操作数据库结构Sql语句
2008-02-11 18:59:00
Python实现简单状态框架的方法
2022-08-20 14:13:44
详解Python爬虫的基本写法
2022-12-26 07:49:45
详解Python中常用的激活函数(Sigmoid、Tanh、ReLU等)
2022-03-25 22:45:51
![](https://img.aspxhome.com/file/2023/5/90445_0s.png)
Pytorch Tensor基本数学运算详解
2022-12-09 03:26:50
重写django的model下的objects模型管理器方式
2023-04-24 15:10:14
python httpx的具体使用
2021-05-31 21:42:28
python 获取图片分辨率的方法
2022-02-19 12:03:56