[Tutor] array and dictionary

"Shantanoo Mahajan (शंत "Shantanoo Mahajan (शंत
Sun Sep 21 08:21:32 CEST 2008


Straight forward method would be...

 >>> a=[[1],[2]]
 >>> b={}
 >>> for x in range(len(a)):
...     b[x] = a[x]
...
 >>> a
[[1], [2]]
 >>> b
{0: [1], 1: [2]}
 >>>

regards,
shantanoo

On 21-Sep-08, at 11:36 AM, Dinesh B Vadhia wrote:

> Hi!  Say, I've got a numpy array/matrix of the form:
>
> [[1 6 1 2 3]
>  [4 5 4 7 0]
>  [2 0 8 0 2]
>  [8 2 6 3 0]
>  [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]]
>
> And, I want to create a dictionary of rows (as the keys) mapped to  
> lists of non-zero numbers in that row ie.
>
> dictionary_non-zeros = {
> 0: [1 6 1 2 3]
> 1: [4 5 4 7]
> 2: [2 8 2]
> ...
> 9: [2 1 5 6]
> }
>
> How do I do this?
>
> Thanks!
>
> Dinesh
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list