newbie question-multidimensional arrays

Discussion in 'Python' started by Doug Jordan, Jun 15, 2004.

  1. Doug Jordan

    Doug Jordan Guest

    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
    Doug Jordan, Jun 15, 2004
    #1
    1. Advertising

  2. Doug Jordan wrote:

    > 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
    Fernando Perez, Jun 15, 2004
    #2
    1. Advertising

  3. Doug Jordan wrote:

    > 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
    Fernando Perez, Jun 15, 2004
    #3
  4. Doug Jordan

    Miki Tebeka Guest

    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.
    --
    -------------------------------------------------------------------------
    Miki Tebeka <>
    The only difference between children and adults is the price of the toys.
    Miki Tebeka, Jun 15, 2004
    #4
  5. Doug Jordan

    Guest

    "Doug Jordan" <> wrote in message news:<Gevzc.30091$>...
    > 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.
    , Jun 15, 2004
    #5
  6. Doug Jordan

    Dan Bishop Guest

    "Miki Tebeka" <> wrote in message news:<>...
    > 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)]
    Dan Bishop, Jun 16, 2004
    #6
  7. Doug Jordan

    Miki Tebeka Guest

    Hello Dan,

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

    Right. Thanks.

    Bye.
    --
    -------------------------------------------------------------------------
    Miki Tebeka <>
    The only difference between children and adults is the price of the toys.
    Miki Tebeka, Jun 16, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. d[ - - ]b

    Multidimensional arrays? anything else?

    d[ - - ]b, May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,672
    AlexS
    May 18, 2004
  2. countofquad
    Replies:
    4
    Views:
    374
    countofquad
    Mar 5, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
  4. Francesco
    Replies:
    2
    Views:
    1,107
    Francesco
    Nov 6, 2009
  5. Replies:
    28
    Views:
    435
    Larry Evans
    Jan 11, 2011
Loading...

Share This Page