Two-dimensional arrays

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon May 26 12:52:06 EDT 2003


>>>>> "Peter" == Peter Slizik <peter.slizik at pobox.sk> writes:

    Peter> I'm working on a problem in which I have to use
    Peter> 2-dimensional array.  I'm sorry, but I feel I should
    Peter> describe it precisely, so the question will be at the end
    Peter> of this mail.

For multi-dimension python lists (heterogeneous contents), use list
comprehensions to initialize the list

  dim1, dim2 = 3,5
  multi = [ [ 0 for col in range(dim2)] for row in range(dim1) ]

if you have homogeneous lists of numbers, and efficiency matters,
you should take a look at Numeric http://www.pfdubois.com/numpy, eg, 

  from Numeric import zeros, Float
  a = zeros( (dim1, dim2), Float)


John Hunter





More information about the Python-list mailing list