How to get indices of a two dimensional list

S

sramaswamy

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.
 
S

Simon Forman

I have a list

x = [0] * 2
x = x * [2]

You're certainly not doing that.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can't multiply sequence by non-int


And even if you *do* do what it looks like you think you want to do
[[0, 0], [0, 0]]

...you don't get what you think you'd get..

So, let's say you do this
[[0, 7], [0, 7], [0, 7], [0, 7]]

... still not what you thought
[[0, 0], [0, 0], [0, 0], [0, 0]]

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.

When you sort out what you're actually trying to do, the answer to your
question may be in this thread:
http://groups.google.ca/group/comp.lang.python/browse_frm/thread/2f82408e1bcaa02e/27925504b2a4eb4a
Thanks in advance.

Hope this helps,
~Simon
 
G

Gerard Flanagan

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

Gerard
 
G

Gerard Flanagan

Gerard said:
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))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top