471,310 Members | 1,135 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Using arrays in Python - problems.

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
F(n)=G05DEF(F_mean,F_sd)
enddo

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,
Gareth.

--

at**************@yahoo.co.uk

665.9238429876 - Number of the Pentium Beast
Oct 23 '07 #1
4 1671

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

Oct 23 '07 #2
ma*********@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

Oct 24 '07 #3
ma*********@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
Oct 24 '07 #4
Thanks very much to those who sent me a reply to my array problem! Its
now working brilliantly!

Best wishes,
Gareth.

--
ec**************@virgin.net
at**************@yahoo.co.uk

665.9238429876 - Number of the Pentium Beast
Oct 24 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Michiel | last post: by
13 posts views Thread by Jeff Melvaine | last post: by
11 posts views Thread by Soeren Sonnenburg | last post: by
4 posts views Thread by CC | last post: by
27 posts views Thread by hacker1017 | last post: by
30 posts views Thread by Ivan Reborin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.