[SciPy-user] variable (undetermined at initialization) dimensionof an array ?

Travis Oliphant oliphant at ee.byu.edu
Fri Aug 22 12:12:59 EDT 2003


Nils Wagner wrote:
> 
> from scipy import *
> a=array([])
> ic = 0
> eps = 1.e-10
> f = 1.0
> i = 0
> while f < eps:
>   ic = ic + 1
>   a.resize((ic,))
>   a[i] = somevalue
>   i = i + 1
> 
> # f will be changed somehow in this loop
> 
> print a
> 
> Is this an effective way to implement it ?
> 

This will work but you are causing a lot of overhead by reallocating the 
array on each pass.  It would be better to allocate larger chunks or use 
list processing and later convert to an array:


List Processing:

a = []
while f < eps:
     ic = ic + 1
     a.append(somevalue)
     i = i+1
a = array(a)

Allocating larger chunks (better handled in a class that allocates large 
chunks of memory and then keeps track of when it's time to allocate a 
new chunk).



-Travis O.






More information about the SciPy-User mailing list