How to get indices of a two dimensional list

Simon Forman rogue_pedro at yahoo.com
Fri Jun 30 01:46:29 EDT 2006


sramaswamy at gmail.com wrote:
> I have a list
>
> x = [0] * 2
> x = x * [2]

You're certainly not doing that.

>>> x = [0] * 2
>>> x = x * [2]
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
>>> x = [[0] * 2] * 2
>>> x
[[0, 0], [0, 0]]

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

So, let's say you do this
>>> x[1][1] = 7
>>> x
[[0, 7], [0, 7], [0, 7], [0, 7]]

.. still not what you thought

>>> x = [[0] * 2] * 4
>>> x
[[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




More information about the Python-list mailing list