Using arrays in Python - problems.

Discussion in 'Python' started by attackwarningred, Oct 23, 2007.

  1. Dear All,
    Hello! I've just started to use Python and its a lovely
    language! I've previously programmed in Fortran 95 and have just began
    to use numpy. I'm having a few problems with arrays in Python though and
    wondered if someone could offer me some advice?
    I wrote the following Fortran code to randomly generate numbers from
    a log-normal distribution for use in a Monte Carlo model:

    do n=1,shotcount

    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.

    Best wishes,
    attackwarningred, Oct 23, 2007
    1. Advertisements

  2. attackwarningred

    marek.rocki Guest

    attackwarningred napisa (a):
    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.rocki, Oct 24, 2007
    1. Advertisements

  3. attackwarningred

    Robert Kern Guest

    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:

    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
    Robert Kern, Oct 24, 2007
  4. attackwarningred

    Duncan Smith Guest


    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 Smith, Oct 24, 2007
  5. Thanks very much to those who sent me a reply to my array problem! Its
    now working brilliantly!

    Best wishes,


    665.9238429876 - Number of the Pentium Beast
    attackwarningred, Oct 24, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.