[Numpy-discussion] lost with slicing

Partridge, Matthew BGI SYD Matthew.Partridge@barclaysglobal....
Mon Mar 30 18:54:45 CDT 2009

I apologise if I'm asking an obvious question or one that has already
been addressed.

I've tried to understand the documentation in the numpy manual on
slicing, but I'm a bit lost.  I'm trying to do indexing using both
slices and index lists.  I have a problem when I do something like:

x[0, :, [0,1,2]]

Here are a couple of examples:

>>> a = numpy.arange(6).reshape(2,3)
>>> print a
[[0 1 2]
 [3 4 5]]
>>> print a[:, [0,1,2]]   # example 1 - this works as I expected
[[0 1 2]
 [3 4 5]]
>>> b = numpy.arange(6).reshape(1,2,3)
>>> print b
[[[0 1 2]
  [3 4 5]]]
>>> print b[0, :, [0,1,2]]  # example 2 - this seems to be the transpose
of what I was expecting
[[0 3]
 [1 4]
 [2 5]]
>>> print b[0, [[0],[1]], [[0,1,2]]] # example 3 - this is what I
[[0 1 2]
 [3 4 5]]

Am I doing something wrong?  Why do we get different behaviour in
example 2 compared with example 1 or example 3?

(I'm using numpy on python 2.4.1 for windows, but I've tried
some more recent versions of numpy as well.)


This message and any attachments are confidential, proprietary, and may be privileged. If this message was misdirected, Barclays Global Investors (BGI) does not waive any confidentiality or privilege. If you are not the intended recipient, please notify us immediately and destroy the message without disclosing its contents to anyone. Any distribution, use or copying of this e-mail or the information it contains by other than an intended recipient is unauthorized. The views and opinions expressed in this e-mail message are the author's own and may not reflect the views and opinions of BGI, unless the author is authorized by BGI to express such views or opinions on its behalf. All email sent to or from this address is subject to electronic storage and review by BGI. Although BGI operates anti-virus programs, it does not accept responsibility for any damage whatsoever caused by viruses being passed.

More information about the Numpy-discussion mailing list