Using arrays in Python - problems.

Robert Kern robert.kern at gmail.com
Wed Oct 24 02:01:05 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)

A more idiomatic version would be this:

  F = numpy.empty((shotcount,), dtype=float)

attackwarningred, you might want to ask your numpy questions on the
numpy-discussion mailing list:

  http://www.scipy.org/Mailing_Lists

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list