[Tutor] Table or Matrix Whatever

Justin Sheehy dworkin@ccs.neu.edu
06 Apr 2000 18:07:33 -0400


Gustavo Passos Tourinho <tourinho@descartes.ucpel.tche.br> writes:

> I would like to know how can i creat a table or matrix. I need to store
> datas like:
> 
>         1         2        3
> q1      a         b       a,b
> q2      b         a        a
> 
> And how can i acess the datas from table like:
> test[2][2]=a

There are several ways.  You could use a dictionary with tuples for
keys:

>>> table = {}
>>> table[('q1', 1)] = 'a'
>>> table[('q1', 2)] = 'b'
>>> table[('q1', 3)] = ('a', 'b')
>>> table[('q2', 1)] = 'b'       
>>> table[('q2', 2)] = 'a'
>>> table[('q2', 3)] = 'a'
>>> table[('q1', 3)]
('a', 'b')
>>> table[('q2', 2)]
'a'
>>> table[('q1', 3)] = 'a'
>>> table[('q1', 3)]
'a'

You could also use nested lists...

-Justin