[Numpy-discussion] Matlab -> NumPy translation and indexing
Wed Mar 14 13:46:05 CDT 2007
On Wed, Mar 14, 2007 at 04:11:43PM +0100, Sturla Molden wrote:
> On 3/14/2007 2:46 PM, Robert Cimrman wrote:
> > a = 
> > while ...
> > a.append( scalar )
> > a = array( a )
> While it may help, growing Python lists is also an O(N) process.
> One can reduce the amount of allocations by preallocating an ndarray of
> a certain size (e.g. 1024 scalars), filling it up, and storing it in a
> linked list. Finally, the stored arrays are retrieved as a single
> contiguous array. Example code below (cf. class scalar_list).
> Sturla Molden
I definitely second this comment. Using arrays when you are trying to
append a lot of data is using the wrong data format. And the code is so
much more readable with lists.
If you want to do something even nicer you might want to use a generator,
possibly one created with the "yield" keyword.
More information about the Numpy-discussion