保存二维numpy数组和矩阵

博客围绕numpy展开,但具体内容缺失。numpy是信息技术领域常用工具,在数据处理等方面有重要作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import numpy as np


# 保存二维数组或矩阵
class MyNumPyExport(object):
    def __init__(self):
        setattr(self, 'file', 'default.out')
        setattr(self, 'numpy_to_export', np.array([]))

    def set_file_name(self, file):
        setattr(self, 'file', file)

    def export(self, nums):
        fid = open(self.file, 'a')  # 追加模式,新增内容增加到末尾
        fid.write('\n[')
        nums_lst = nums.tolist()
        for ite in nums_lst[0:-1]:
            cell = ','.join(str(p) for p in ite)
            fid.write('[' + cell + '],\n')
        cell = ','.join(str(p) for p in nums_lst[-1])
        fid.write('[' + cell + ']]\n')
        fid.close()


if __name__ == '__main__':
    test_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    test_matrix = np.matrix([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

    ins_mynp_exp = MyNumPyExport()
    ins_mynp_exp.set_file_name('test.txt')
    ins_mynp_exp.export(test_array)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值