Using arrays in Python - problems.

marek.rocki at wp.pl marek.rocki at wp.pl
Wed Oct 24 01:14:30 CEST 2007


attackwarningred napisa (a):

> The array F(n) is dynamically allocated earlier on and is sized with
> reference to shotcount, the number of iterations the model performs. The
> problem is I can't get something like this to run in Python using numpy,
> and for the size of the array to be sized dynamically with reference to
> the variable shotcount. I acknowledge that my knowledge of Python is
> still really basic (I only started learning it a few days ago) and I'm
> trying to get out of the Fortran programming mindset but I'm stuck and
> don't seem to be able to get any further. If anyone could help I'd be
> really grateful. Thanks very much in advance.

Hello. If you want your array to be dynamically resized at every loop
iteration, that might be quite inefficient. How about initialising it
with a required size?

F = numpy.array([0]*shotcount)
for n in xrange(shotcount):
        F[n] = random.lognormvariate(F_mean, F_sd)

Hope that helps,
Marek




More information about the Python-list mailing list