Using arrays in Python - problems.

Duncan Smith buzzard at urubu.freeserve.co.uk
Wed Oct 24 03:05:24 CEST 2007


marek.rocki at wp.pl wrote:
> 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
> 

or,

F = numpy.random.lognormal(F_mean, F_sd, shotcount)

(assuming F_mean, F_sd are the parameters of the distribution, rather
than the actual mean and standard deviation).

Duncan



More information about the Python-list mailing list