vector subscripts in Python?

Fernando Perez fperez528 at yahoo.com
Thu Jun 12 01:47:11 CEST 2003


beliavsky at aol.com wrote:

> Does Python have the equivalent of "vector subscripts" of Fortran 95?
> The code below illustrates what I am looking for -- with better syntax.
> 
> def subscript(xx,ii):
>     # return the elements in xx subscripted by ii
>     y = []
>     for i in ii: y.append(xx[i])
>     return y
>     
> ii = [0,2]
> xx = [1,4,9]
> print subscript(xx,ii) # returns [1, 9]; in F95, "print*,xx(ii)" is analogous

Using Numeric:

In [5]: xx = array([1,4,9])

In [6]: ii = [0,2]

In [7]: take(xx,ii)
Out[7]: array([1, 9])

For Fortran95-like behavior, you need to use Numeric.

Best,

f.




More information about the Python-list mailing list