[SciPy-user] list of arrays

Trevis Crane t_crane at mrl.uiuc.edu
Fri May 25 15:13:54 EDT 2007


> -----Original Message-----
> From: scipy-user-bounces at scipy.org
[mailto:scipy-user-bounces at scipy.org] On
> Behalf Of Ryan May
> Sent: Friday, May 25, 2007 1:57 PM
> To: SciPy Users List
> Subject: Re: [SciPy-user] list of arrays
> 
> Trevis Crane wrote:
> > Hi,
> >
> >
> >
> > If I have a list of arrays, what's the best way of getting the last
> > element of each array without using a for-loop?
> >
> 
> [array[-1] for array in list]

[Trevis Crane] 

I know I'm often dense, but I'm unsure how to implement this suggestion.
Here's some code to clarify what I mean:

from numpy import *
a = array([1,2,3])
b = array([4,5,6])
c = array([7,8,9])

d = []
d.append(a)
d.append(b)
d.append(c)

Now, d is a list containing three arrays, a,b and c.  I assume you
suggest something like this:

d[array[-1] for array in d]

But this returns a syntax error.  I'm sure I'm missing something but not
sure what.

What I want is the output of the following for-loop

e = zeros(len(d))
for j in range(len(d))
	e[j] = d[j][-1]

This gives me e, which obviously is an array containing the last
elements of each of the arrays contained in the list.  
	
thanks,
trevis






> 
> (assuming the arrays are 1-D, otherwise use array.flatten()
> 
> Ryan
> 
> --
> Ryan May
> Graduate Research Assistant
> School of Meteorology
> University of Oklahoma
> 
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user



More information about the SciPy-User mailing list