[Tutor] Re: [Python] Primes on a two-dimensional plane in the form of a rectangular spiral

Lloyd Hugh Allen lha2@columbia.edu
Mon, 09 Apr 2001 10:23:52 -0400


Because the original post (from Julieta) was encoded by MIME, the
formatting got all messed up and I won't be quoting from it.

See the bottom of <http://www.ibiblio.org/obp/thinkCSpy/chap08.htm> for
how to create a matrix, which can be thought of as a two-dimensional
list; the problem is that negative indices won't work the way you want
them to. A negative index will count from the end of the list; so 

matrix=[[1,2,3],[4,5,6],[7,8,9]]

is thought of by people as looking like

[1,2,3]
[4,5,6]
[7,8,9]

and matrix[-1][-1] should return <9>.

You could use a matrix implementation, and keep track of where the
origin is, but that could get ugly. Because you have negative indices,
it may be easier for you to pretend that you have a sparse matrix (even
though you will have zero entries with value zero) and use a dictionary
with key-values that are tuples: for the above matrix, with <5> as the
"origin",

matrixdict = {(0,0):5, (0,1):6}    #you can set initial values
matrixdict[(-1,1)] = 1              #and you can add values as you go
matrixdict[(-1,0)] = 4
matrixdict
{(0, 1): 6, (0, 0): 5, (-1, 1): 1, (-1, 0): 4}

matrixdict[(0,1)]
6

This implementation looks easier and more appropriate for your project.