简体   繁体   English

在python中存储稀疏矩阵时,如何更改表示形式?

[英]How do I change the representation while storing a sparse matrix in python?

The sparse matrix representation is of the form: 稀疏矩阵表示形式为:

          (i,j)       value

I want to store these values to a text file in 3 columns namely: 我想将这些值存储到3列的文本文件中:

         i            j               value

If your sparse matrix is stored in a dictionary called dict then you can use: 如果您的稀疏矩阵存储在字典dict中,则可以使用:

f = open('output.txt', 'w')
for (i, j), value in dict.items():
    f.write("%s\t%s\t%s\n" % (i, j, value))

This will output the data as a tab-delimited text file. 这会将数据输出为制表符分隔的文本文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM