numpy 在Python中打印文本之间的矩阵

qxgroojn  于 2023-01-26  发布在  Python
关注(0)|答案(3)|浏览(108)

我在这里看到了一些例子,其中解释了如何将矩阵打印到.txt文件。但是,我还没有找到一个简单的方法来执行我想做的事情,即打印几个用.txt编写的矩阵,其中还打印了附加文本。注意,这些文本并不意味着是标题或其他内容,因此使用 Dataframe 的解决方案并不真正适合。
假设我有以下矩阵:

M1 = np.zeros((6, 6))
M2 = np.zeros((6, 6))
M3 = np.zeros((6, 6))

我想打印相对于顶部行的矩阵

with open("example.txt", "w") as f:
    f.write("-------  MATRIX 1 ------------------------------------------------------------\n")    
    f.write("------- MATRIX 2 ---------------------------------------------------------\n")
    f.write("------- MATRIX 3 ---------------------------------------------------------\n")

我所期望的结果将是一个.txt文件,类似于以下内容:

-------  MATRIX 1 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
-------  MATRIX 2 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
-------  MATRIX 3 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
ajsxfq5m

ajsxfq5m1#

对于这种情况,使用numpy.savetxt是适当的选择;特定参数允许设置 * 格式 标题 * 和 * 注解 *:

with open('test.txt', "w") as f:
    np.savetxt(f, M1, fmt='%d', delimiter='   ', comments='', header="-------  MATRIX 1 ------------------------------------------------------------\n")
    np.savetxt(f, M2, fmt='%d', delimiter='   ', comments='', header="-------  MATRIX 2 ------------------------------------------------------------\n")
    np.savetxt(f, M3, fmt='%d', delimiter='   ', comments='', header="-------  MATRIX 3 ------------------------------------------------------------\n")
fkaflof6

fkaflof62#

如果你真的只想打印这3行,你可以使用这个。分隔符是你可以用来分隔的,它是一个白色作为默认值,这里我用3来匹配你想要的。
对于某些可用于格式化的参数,请选中here

import numpy as np

M1 = np.zeros((6, 6))
M2 = np.zeros((6, 6))
M3 = np.zeros((6, 6))

with open("C:/temp/test.txt", "w") as f:
    f.write("-------  MATRIX 1 ------------------------------------------------------------\n")    
    np.savetxt(f, M1, fmt='%g', delimiter='    ')
    f.write("\n-------  MATRIX 2 ------------------------------------------------------------\n")    
    np.savetxt(f, M2, fmt='%g', delimiter='    ')
    f.write("\n-------  MATRIX 3 ------------------------------------------------------------\n")    
    np.savetxt(f, M3, fmt='%g', delimiter='    ')

此代码生成以下内容:

-------  MATRIX 1 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0

-------  MATRIX 2 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0

-------  MATRIX 3 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0yycz8jy

0yycz8jy3#

我对numpy不太熟悉,不知道内置的解决方法,但是您可以得到如下输出:

M1 = np.zeros((6, 6))
M2 = np.zeros((6, 6))
M3 = np.zeros((6, 6))
   
with open('example.txt', 'w') as f:
    for idx, matrix in enumerate((M1, M2, M3)):
        f.write(f'------- MATRIX {idx+1} ------------------------------------------------------------\n')
        for matrix_row in matrix:
            row ='    '.join(str(i) for i in matrix_row) + '\n'
            f.write(row)

输出:

------- MATRIX 1 ------------------------------------------------------------
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
------- MATRIX 2 ------------------------------------------------------------
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
------- MATRIX 3 ------------------------------------------------------------
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
0.0    0.0    0.0    0.0    0.0    0.0
  • 如果必须获取0而不是0.0,则可以先将值转换为int,然后再转换为str,如下所示:
row ='    '.join(str(int(i)) for i in matrix_row) + '\n'

或在numpy数组中定义值类型:

M1 = np.zeros((6, 6), dtype=int)
M2 = np.zeros((6, 6), dtype=int)
M3 = np.zeros((6, 6), dtype=int)

以获得:

------- MATRIX 1 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
------- MATRIX 2 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
------- MATRIX 3 ------------------------------------------------------------
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0
0    0    0    0    0    0

相关问题