Newbie: How do I implement an 2 element integer array in Python.?

Steven D. Majewski sdm7g at virginia.edu
Wed Oct 11 12:20:07 EDT 2000


You can also use dictionaries indexed by two element tuples:

>>> array = {}
>>> array[1,2] = (1,2)
>>> array[2,2] = None
>>> print array[1,2]
(1, 2)

This is handy for small multi-dimensional arrays, very large but 
sparse arrays, or anyplace where you might want to catch a reference
to an uninitialized item:

>>> print array[0,0]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: (0, 0)


Or use UserDict and  {}.get with a default arg to make a sparse
array that returns zero for uninitialized values. 


---|  Steven D. Majewski   (804-982-0831)  <sdm7g at Virginia.EDU>  |---
---|  Department of Molecular Physiology and Biological Physics  |---
---|  University of Virginia             Health Sciences Center  |---
---|  P.O. Box 10011            Charlottesville, VA  22906-0011  |---
		"All operating systems want to be unix, 
		 All programming languages want to be lisp." 





More information about the Python-list mailing list