详解Python list和numpy array的存储和读取方法
作者:sjtu_leexx 时间:2022-05-04 05:58:10
numpy array存储为.npy
存储:
import numpy as np
numpy_array = np.array([1,2,3])
np.save('log.npy',numpy_array )
读取:
import numpy as np
numpy_array = np.load('log.npy')
运行结果:
list存储为.txt
存储:
list_log = []
list_log.append([1,2,3])
list_log.append([4,5,6,7])
file= open('log.txt', 'w')
for fp in list_log:
file.write(str(fp))
file.write('\n')
file.close()
这样存储的结果list_log的每一行在txt也是分行的
运行结果:
读取:
file=open('log.txt', 'r')
list_read = file.readlines()
读出来list_read的结果仍然是一行一行的
运行结果:
.txt文件读取为int
label_path = 'C:/Users/leex/Desktop/label.txt'
file = open((label_path),'r')
label = [int(x.strip()) for x in file]
file.close()
运行结果:
如果不加int(),则读取的为字符串格式
还有一种常见的情况是label是以one-hot编码存储的
可以用np.loadtxt读取
import numpy as np
label_path = 'C:/Users/leex/Desktop/label.txt'
label = np.loadtxt(label_path, dtype=np.int64)
运行结果
来源:https://blog.csdn.net/sjtuxx_lee/article/details/80081136
标签:Python,list,numpy,array
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
纯CSS实现动态提示信息Tooltips
2008-05-22 11:56:00
Python处理命令行参数模块optpars用法实例分析
2021-07-20 05:27:36
Python爬取网站图片并保存的实现示例
2023-06-05 18:01:29
![](https://img.aspxhome.com/file/2023/3/89373_0s.jpg)
在ASP.NET2.0通过SMTP的验证发送EMAIL
2007-09-23 12:29:00
php中fgetcsv()函数用法实例
2023-06-15 05:50:37
Python Pandas删除替换并提取其中的缺失值NaN(dropna,fillna,isnull)
2023-08-11 01:11:34
复化梯形求积分实例——用Python进行数值计算
2021-10-08 13:03:26
![](https://img.aspxhome.com/file/2023/6/79716_0s.jpg)
Oracle数据库的备份及恢复策略研究
2010-07-16 12:54:00
在SQL server2005数据库下创建计划任务
2008-12-26 09:19:00
![](https://img.aspxhome.com/file/UploadPic/200812/20081231122259529.jpg)
Pandas 模糊查询与替换的操作
2023-05-27 22:33:38
![](https://img.aspxhome.com/file/2023/3/104863_0s.jpg)
python 读取dicom文件,生成info.txt和raw文件的方法
2021-06-26 14:45:50
Jmeter并发执行Python 脚本的完整流程
2021-05-12 02:52:49
python实现字符串和日期相互转换的方法
2022-09-02 04:05:55
Python二次规划和线性规划使用实例
2023-08-28 05:37:10
用ASP和XMLHTTP分析远程XML文件
2007-12-12 12:48:00
深入剖析SQL Server的六种数据移动方法
2009-01-07 14:09:00
交互设计师的尴尬-我的强项是什么?
2008-12-29 14:00:00
Python使用ntplib库同步校准当地时间的方法
2021-10-07 13:55:32
基于Django signals 信号作用及用法详解
2021-08-01 10:59:52
![](https://img.aspxhome.com/file/2023/1/87991_0s.jpg)
深入解析python中的实例方法、类方法和静态方法
2022-07-23 09:05:57
![](https://img.aspxhome.com/file/2023/8/72828_0s.png)