How to get indices of a two dimensional list

Gerard Flanagan grflanagan at yahoo.co.uk
Fri Jun 30 05:22:09 EDT 2006


Gerard Flanagan wrote:
> sramaswamy at gmail.com wrote:
> > I have a list
> >
> > x = [0] * 2
> > x = x * [2]
> > x[1,1] = 7
> >
> > This gives me the x value
> > [[0,0] [0,0] [0,0] [0,7]]
> >
> > I want to get the indices of the value 7.
> > i.e. something like
> > i = a.index(max(a)) gives me '1'
> >
> > This only gives me the index in one dimension. Is there any method by
> > which  I can get (1,1) as the answer.
> >
> > Thanks in advance.
>
> If you post code that doesn't work, people won't treat you seriously.
> I have some code that might help if you'd like to try again.
>
> All the best
>

Ok, that was mean-spirited.  The code is below - it's related to
another problem but maybe it can be adapted to your own. Good luck!

def irows(n):
    i=0
    while True:
        yield divmod(i,n)
        i += 1

x = [0,0,0,7,0,0,0,0]

d = dict(zip(enumerate(x), irows(2)))

assert d[x.index(7),7] == (1,1)
assert d[x.index(0),0] == (0,0)

d = dict(zip(irows(2),x))

assert d[1,1] == 7

sudoku = dict(zip(irows(9),[[0]]*81))




More information about the Python-list mailing list