newbie question-multidimensional arrays

beliavsky at beliavsky at
Tue Jun 15 14:42:04 CEST 2004

"Doug Jordan" <djordan8 at> wrote in message news:<Gevzc.30091$KL2.23881 at>...
> How do you define and access multidimensional arrays in Python?   I am new
> to python and come from a C and FORTRAN background and I am not sure how to
> define an array without giving the contents of the array.  Any help will be
> appreciated.   I would like to be able to read and manipulate
> multidimensional arrays using a nested loop.  I cannot find anything in the
> documentation to help me.
> Doug

As another poster suggested, try Numeric or Numarray. With Numeric,
one typically allocates a new array to have all zero values with a
statement such as

x = zeros([nrows,ncol],Float)

With Numeric in Python and in other matrix-oriented languages like
Matlab and Fortran 90/95, you usually do NOT want to manipulate arrays
using nested loops. Instead, whole-array operations are typically
faster, especially in an interpreted language like Python, as well as
being easier to code and read.

For example, if x and y are two arrays of the same size, you can
create a new array whose elements are the sum of corresponding
elements of x and y by writing just

z = x + y

There is online documentation for Numeric and Numarray. A good printed
reference for Numeric (and the rest of Python) is the "Python in a
Nutshell" by Martelli.

More information about the Python-list mailing list