You can use vocabulary and tuples to access data:
>>> size = (4,4)
>>> mat = {}
>>> mat[0,1] = 3
>>> mat[2,3] = 5
>>> for i in range(size[0]):
for j in range(size[1]):
print mat.get((i,j), 0) ,
print
0 3 0 0
0 0 0 0
0 0 0 5
0 0 0 0
Of course, you should make a class for this and add the methods you need:
class Sparse(dict):
pass
BTW, you can also use scipy.sparsefrom scipy lib
source
share