My first stumbling block with Python

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Aug 22 10:36:44 EDT 2002


On Thursday 22 Aug 2002 1:12 pm, Mr. Neutron wrote:
> Sigh, it was too good to be true. I have come across my first problem
> with Python programming.
>
> Python does not seem to have an Array type. It has the Array object,
> which is a one dimensional array, and it works fine. However, I need a
> two dimensional array mapped (X,Y) of tuples.
>
> I can think of ways to do this in the language, by creating a list of all
> the elements, and creating a function def GetPos(X,Y) that maps into the
> list and gets the element. The math isn't too hard off the top of my head
> it is like
> 	GetPos(X,Y):
> 		return list[ (Y * RowSize) + X ]
>
> 	and SetPos(X,Y, MyTuple)
> 		list[ (Y*RowSize) + X ] = MyTuple
>
> Where RowSize is the width of the array.
>
> Now the question is, are there any easier or better ways to get a two
> dimensional array of tuples in Python
>
> Something like
>
> 		MyArray[X][Y] = (tuple)


Unless I'm being really stupid! could this be of any help:-

Python 2.2.1 (#3, May 29 2002, 20:32:44)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[[1, 1, 1, 1, 1],
...    [2, 2, 2, 2, 2],
...    [3, 3, 3, 3, 3],
...    [4, 4, 4, 4, 4]]
>>>
...
>>>
>>> a
[[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4]]
>>> a[1][1]
2
>>> a[1][1]=15
>>> a[1][1]
15
>>> a.append([5, 5, 5, 5, 5])
>>> a
[[1, 1, 1, 1, 1], [2, 15, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 
5, 5, 5]]
>>> len(a[0])
5


If I am being silly then you could also take a look at the array built in 
module:-

pydoc array

Python Library Documentation: module array

NAME
    array

FILE
    /usr/local/lib/python2.2/lib-dynload/array.so

DESCRIPTION
    This module defines a new object type which can efficiently represent
    an array of basic values: characters, integers, floating point
    numbers.  Arrays are sequence types and behave very much like lists,
    except that the type of objects stored in them is constrained.  The
    type is specified at object creation time by using a type code, which
 <snip>


Or create your own array class.....


HTH
Martin











More information about the Python-list mailing list