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.
精彩评论