[Tutor] Problem with tuple and dictionary

Kirby Urner urnerk@qwest.net
Tue, 13 Nov 2001 16:53:51 -0800


At 05:02 PM 11/13/2001 -0700, Mike Yuen wrote:
>I'm writing a larger program and for some reason,
>I keep getting an error
>at the following:
>
>matrix[(curr,last)] = int(diff)

You should cut and past the error you're getting.

It's OK to use a tuple object as a dictionary key:

   >>> matrix = {}
   >>> t = (1,2)
   >>> matrix[t] = 3
   >>> matrix
   {(1, 2): 3}

I assume you don't mean your matrix to be a 2-dimensional
list, in which case it'd be more like:

   >>> matrix = [[0 for j in range(2)] for i in range(2)]
   >>> matrix
   [[0, 0], [0, 0]]
   >>> matrix[1][1]=3
   >>> matrix[0][1]=2
   >>> matrix
   [[0, 2], [0, 3]]

Kirby