开发者

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 开发者_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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜