[SciPy-user] what different between array([ 1., 2., 3.]) and array([[ 1., 2., 3.]])

Sunghyun Cho sodomau at postech.ac.kr
Mon Nov 27 06:53:06 CST 2006


Hi
The array that you generated first is a "1-D" array.
It means you should use only one index to retrieve a value in the array.
If you want to access the first element of the array, you can do it in the
way below.
 ' print x[0] '
And after assigning (1,3) into x.shape, it changed to a 2-D array.
So you can use two indices.
'x[0,:]' in your code means that you want to access whole elements of the
first row(the 1st dimension of a 2D array) of  x.

2006/11/27, Zhang Sam <hollowspook at gmail.com>:
>
> Hi, there
>
> I am a newer here. I just used numpy and scipy for a while.
> I am confused about the 1-D array.
>
> The code is as follows:
>
> ---------------------------------------------------------------------------------------------------
> >>> x=array([1 ,2.,3])
> >>> x
> array([ 1.,  2.,  3.])
> >>> x.shape
> (3,)
> >>> x[0,:]
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> IndexError: too many indices
> >>> x.shape = 1,3
> >>> x
> array([[ 1.,  2.,  3.]])
> >>> x[0,:]
> array([ 1.,  2.,  3.])
>
> -----------------------------------------------------------------------------------------------------
>
> Typing x=array([1 ,2.,3])  will generate array([ 1.,  2.,  3.]). Since
> this is a array variable, It should be indexd by list. However  x[0,:] is
> wrong.
> I must force its shape to (1,3), then x[0,:] will return a array.
>
> Both x (before and after) is array class. Why the indexing method exist
> difference.
> What's difference between them?
>
> If there is explaination in some document already, a link to it is ok.
> Please do me a favour.
>
>
> Thanks in advance.
>
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
>
>


-- 
Sunghyun Cho
sodomau at postech.ac.kr
http://sodomau.plus.or.kr
Computer Graphics Lab.
POSTECH
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20061127/a95c67b5/attachment.html 


More information about the SciPy-user mailing list