[SciPy-User] How to get the index iterator of a scipy sparse matrix?
Warren Weckesser
warren.weckesser@enthought....
Mon Mar 26 17:25:39 CDT 2012
On Sun, Mar 25, 2012 at 5:47 PM, Pengkui Luo <pengkui.luo@gmail.com> wrote:
> e.g. suppose A is a scipy lil sparse matrix, and the result of print(A) is:
>
> (0, 1) 1.0
> (0, 2) -1.0
> (1, 0) 1.0
> (1, 2) -1.0
> (2, 1) 2.0
>
> How can I get an iterator (or at least a list) of these (i, j) index pairs?
>
> Thanks!
>
>
You could convert the matrix to DOK format and get the keys:
In [1]: from scipy.sparse import lil_matrix
In [2]: a = lil_matrix([[0,0,0],[0,10,0],[20,0,30]])
In [3]: a.todok().keys()
Out[3]: [(2, 0), (1, 1), (2, 2)]
In [4]: a.todense()
Out[4]:
matrix([[ 0, 0, 0],
[ 0, 10, 0],
[20, 0, 30]])
That is not the most efficient method, but it is certainly easy to
implement.
Warren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120326/359be4ee/attachment.html
More information about the SciPy-User
mailing list