tensor
x = torch.rand(4,5) torch.save(x, "myTensor.pth") y = torch.load("myTensor.pth") print(y)
list
保存到本地就是保存为.npy文件a = [(u'9000023330249', 1), (u'13142928', 1), (u'9000084906496', 1)] # 保存 import numpy as np a=np.array(a) np.save('a.npy',a) # 保存为.npy格式 # 读取 b=np.load('a.npy') b=b.tolist() b
保存为txt文件
就是直接打开txt文件,往里写
file = open('file_name.txt','w'); file.write(str(list_variable)); file.close();
numpy array
np.save("filename.npy",a) b = np.load("filename.npy")
dict
如果字典中的项有numpy.array, 需要.tolist()一下jsObj = json.dumps(self.dict) fileObject = open('dict.json', 'w') fileObject.write(jsObj) fileObject.close()