Error in Following python program

MRAB python at mrabarnett.plus.com
Sat Sep 4 20:46:23 CEST 2010


On 04/09/2010 19:28, Pramod wrote:
> #/usr/bin/python
> from numpy import matrix
> n=input('Enter matrix range')
> fr=open('mat.txt','r')
> print ('Enter elements into the matrix\n')
> a=matrix([[input()for j in range(n)] for i in range(n)])
> for i in range(n):
>          for j in range(n):
>                  print a[i][j]
>          print '\n'
>
> When i run the above program the following error is Coming please
> Error is
>                                             Enter matrix range3
> Enter elements into the matrix
>
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> [[1 2 3]]
> Traceback (most recent call last):
>    File "2.py", line 10, in<module>
>      print a[i][j]
>    File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
> line 265, in __getitem__
>      out = N.ndarray.__getitem__
>
> please resolve my problem  Thanks in advance
> ~
The matrix is 2-dimensional, which in numpy is means you need to write:

     a[i, i]

not:

     a[i][j]

and no, they're not the same! :-)



More information about the Python-list mailing list