使用python将二维numpy ndarray写入到文件中
a = np.random.random((5, 4))# ndarray转为listb = a.tolist()# 二维数组遍历,列表生成式c = [[i for i in j] for j in b]with open("a.txt", "w") as fp:# 二维数组遍历,列表生成式, 省去写for循环# "\n".join表示把每一行之后加入换行符# " ".join表示把每一行不同字符之间加入空格fp.writelines("\n".join([" ".join([str(i) for i in j]) for j in b]))
a.txt的结果
如果觉得《python 二维数组写txt文件 二维ndarray写txt》对你有帮助,请点赞、收藏,并留下你的观点哦!