[Numpy-discussion] Matching 0-d arrays and NumPy scalars

Stefan van der Walt stefan at sun.ac.za
Thu Feb 21 18:17:15 EST 2008


On Thu, Feb 21, 2008 at 12:08:32PM -0500, Alan G Isaac wrote:
> On Thu, 21 Feb 2008, Konrad Hinsen apparently wrote:
> 
> > What I see as more fundamental is the behaviour of Python container 
> > objects (lists, sets, etc.). If you add an object to a container and 
> > then access it as an element of the container, you get the original 
> > object (or something that behaves like the original object) without 
> > any trace of the container itself. 
> 
> I am not a CS type, but your statement seems related to
> a matrix behavior that I find bothersome and unnatural::
> 
>     >>> M = N.mat('1 2;3 4')
>     >>> M[0]
>     matrix([[1, 2]])
>     >>> M[0][0]
>     matrix([[1, 2]])

This is exactly what I would expect for matrices: M[0] is the first
row of the matrix.  Note that you don't see this behaviour for
ndarrays, since those don't insist on having a minimum of
2-dimensions.

In [2]: x = np.arange(12).reshape((3,4))

In [3]: x
Out[3]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [4]: x[0][0]
Out[4]: 0

In [5]: x[0]
Out[5]: array([0, 1, 2, 3])

Regards
Stefan



More information about the NumPy-Discussion mailing list