# [Tutor] Re: indexing lists

Alan Gauld alan.gauld@gssec.bt.co.uk
Tue, 6 Jun 2000 15:49:41 +0100

```> I'm wondering how, if even possible, you'd find the
> index of a given number in the following list of lists:
>
> l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Lets assume you use:

l = [[0,1,2],[3,4,5],[6,7,8]]

Now the 'row' is N/3 - ie 7 is in row 2, 5 in row 1 etc

the position within the row is index(n) of the row,
thus:

row = l[int(N)/3] # force integer division
item = row.index(N)

> simpler [1, 2, 3, 4, 5, 6, 7, 8, 9] structure.

Another approach ius to use a dictionary.
brd = {	1:[0,[(2,3),(4,7),(5,9)]],
2:[0,[(1,3),(5,8)]],
3:[0,[(1,2),(6,9),(5,7)]], etc...
}

Where the content of the dictionary consists of:
+ the content of the cell
- unused = 0, 'X' = 1, 'O' = 2 say
+ a list of rows for that cell
- thus cell one has 3 rows formed by:
1,2,3 / 1,4,7 / 1,5,9

Or you could create a cell object to hold those things
as fields - probably nicer:

class cell:
def __init__(self, rows, value=0):
self.value = value
self.rows = rows

Now create a list of cells:

targets = [(1,2,3),(4,5,6),(7,8,9), # horizontals
(1,4,7),(2,5,6),(3,6,9), # verticals
(1,5,9),(3,5,7)]	    # diagonals

brd = []
rows = []
for i in range(1:10):
# select the rows for this cell
for t in targets:
if i in t: rows.append(t)
brd.append(Cell(rows))

You can then update the contents of a cell and extract
the list of rows to check for that cell...

errr maybe,
something like that might work :-)

Alan G.

--
=================================================
This post represents the views of the author
and does not necessarily accurately represent
the views of BT.

```