newbie question-multidimensional arrays

D

Doug Jordan

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
 
F

Fernando Perez

Doug said:
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.

google for: Numpy, Scipy, weave, f2py, numarray

scipy is the grand wrapper which will give you on top of Numeric/Numarray (the
array library proper) a ton of other stuff: special functions, numerical
methods, fortran code wrapping, and out-of-the box wrappers to most/all of blas
and lapack.

For plotting/visualization (which you'll eventually need), matplotlib,
gnuplot.py and MayaVi should be on your list.

For PostScript/LaTeX algorithmic generation, PyX is an amazing tool.

Cheers,

f
 
F

Fernando Perez

Doug said:
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.

google for: Numpy, Scipy, weave, f2py, numarray

scipy is the grand wrapper which will give you on top of Numeric/Numarray (the
array library proper) a ton of other stuff: special functions, numerical
methods, fortran code wrapping, and out-of-the box wrappers to most/all of blas
and lapack.

For plotting/visualization (which you'll eventually need), matplotlib,
gnuplot.py and MayaVi should be on your list.

For PostScript/LaTeX algorithmic generation, PyX is an amazing tool.

Cheers,

f
 
M

Miki Tebeka

Hello Doug,
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.
Python lists are arrays that can hold anything including other lists.
This makes them a multidimensional arrays as well.

For example:
def gen_2darray(m, n, initial=0):
'''Generate two dimensional array

We can't use [[] * n] * m since the internal arrays will point to
the same array.
'''
arr = [None] * m
for i in range(m):
arr = [initial] * n
return arr

a = gen_2darray(10, 10)
a[4][5]

If you need fast computation with arrays have a look at numpy project.

HTH.
 
B

beliavsky

Doug Jordan said:
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.
 
D

Dan Bishop

Miki Tebeka said:
Hello Doug,
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.

Python lists are arrays that can hold anything including other lists.
This makes them a multidimensional arrays as well.

For example:
def gen_2darray(m, n, initial=0):
'''Generate two dimensional array

We can't use [[] * n] * m since the internal arrays will point to
the same array.
'''
arr = [None] * m
for i in range(m):
arr = [initial] * n
return arr


In Python 2.0(?) and later, this can be written as

return [[initial] * m for i in xrange(n)]
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top