开发者

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

开发者 https://www.devze.com 2023-02-15 10:30 出处:网络
The sparse matrix representation is of the form: (i,j)value I want to store these values 开发者_JS百科to a text file in 3 columns namely:

The sparse matrix representation is of the form:

          (i,j)       value

I want to store these values 开发者_JS百科to a text file in 3 columns namely:

         i            j               value


If your sparse matrix is stored in a dictionary called dict then you can use:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消