[Numpy-discussion] array indices and dictionary
Dinesh B Vadhia
dineshbvadhia@hotmail....
Sun Sep 21 12:28:35 CDT 2008
Hi! Say, I've got a numpy array/matrix of the form:
A =
[[1 6 1 2 3]
[4 5 4 7 0]
[2 0 8 0 2]
[0 0 0 3 7]
[0 7 0 3 5]
[8 0 3 0 6]
[8 0 0 2 2]
[3 1 0 4 0]
[5 0 8 0 0]
[2 1 0 5 6]]
I want to create a dictionary of row indexes (as the keys) mapped to lists of the column indexes of non-zero numbers in that row ie.
dictionary_non-zeros = {
0: [0 1 2 3 4]
1: [0 1 2 3]
2: [0 2 4]
3: [3 4]
...
9: [0 1 3 4]
}
How can I do this?
This code ...
d = {}
for number, row in enumerate(A):
d[number] = [value for value in row if value !=0]
... picks up the non-zero values in each row eg.
d = {0: [1, 6, 1, 2, 3], 1: [4, 5, 4, 7], 2: [2, 8, 2], 3: [3, 7], ... 9: [2, 1, 5, 6]}
But, I want to pick up the column index of non-zero elements per row.
Thanks!
Dinesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080921/c6f7366c/attachment.html
More information about the Numpy-discussion
mailing list