[SciPy-user] "dynamic" indexing
Gerard Vermeulen
gerard.vermeulen at grenoble.cnrs.fr
Thu Jun 10 03:48:58 EDT 2004
On Wed, 9 Jun 2004 15:27:32 -1000
"Addison, Jason" <JADDISON at SYSTEMS.TEXTRON.com> wrote:
> How can I take a slice from an array when I do not know before hand the
> dimension of the source array? For example:
>
> # slice is genereated dynamically, its length is not known before hand
> slice = [(2,4),(1,4),(0,15)]
> # then I'd like to somehow use slice to slice an array
> a[slice] -> a[2:4,1:4,0:15]
>
Python-2.3 allows you to do
packer at zombie:~> python
Python 2.3+ (#1, Jan 7 2004, 09:17:35)
[GCC 3.3.1 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scipy import *
>>> a = arange(35)
>>> a.shape = (7, 5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34]])
>>> s0 = slice(0, 8, 2)
>>> s1 = slice(0, 6, 2)
>>> a[s0, s1]
array([[ 0, 2, 4],
[10, 12, 14],
[20, 22, 24],
[30, 32, 34]])
>>>
See http://docs.python.org/whatsnew/section-slices.html
Gerard
More information about the SciPy-User
mailing list