# locate items in matrix (index of lists of lists)

Discussion in 'Python' started by Alexzive, Mar 20, 2009.

1. ### AlexziveGuest

Hello there,

let's suppose I have the following matrix:

mat = [[1,2,3], [3,2,4], [7,8,9], [6,2,9]]

where [.. , .. , ..] are the rows.

I am interested into getting the "row index" of all the matrix rows
where a certain number occurs.
For example for 9 I should get 2 and 3 (starting from 0).
For 10 I should get an error msg (item not found) and handle it.

How to get the"row indexes" of found items?

In practice I am looking for an equivalent to "list.index(x)" for the
case "lists of lists"

Many Thanks!
Alex

PS: this is just a simplified example, but I have actually to deal
with large matrices [~500000 * 4]

Alexzive, Mar 20, 2009

2. ### Chris RebertGuest

On Fri, Mar 20, 2009 at 3:50 AM, Alexzive <> wrote:
> Hello there,
>
> let's suppose I have the following matrix:
>
> mat = [[1,2,3], [3,2,4], [7,8,9], [6,2,9]]
>
> where [.. , .. , ..] are the rows.
>
> I am interested into getting the "row index" of all the matrix rows
> where a certain number occurs.
> For example for 9 I should get 2 and 3 (starting from 0).
> For 10 I should get an error msg (item not found) and handle it.
>
> How to get the"row indexes" of found items?

indices = [i for i, row in enumerate(mat) if item in row]

where item is 9, 10, or whatever you're looking for.
If the item is not present in any of the sublists, indices will be empty.

Also, if you're doing lots of matrix work, you might want to look into

Cheers,
Chris

--
I have a blog:
http://blog.rebertia.com

Chris Rebert, Mar 20, 2009

3. ### Alessandro ZivelonghiGuest

Many Thanks guys!

and what if I need to look ONLY into the second and third columns,
excluding the first item of each rows?

for example if x = 3 I need to get [0] and not [0,1]

many thanks, Alex

2009/3/20 Tino Wildenhain <>:
> Alexzive wrote:
>>
>> Hello there,
>>
>> let's suppose I have the following matrix:
>>
>> mat = [[1,2,3], [3,2,4], [7,8,9], [6,2,9]]
>>
>> where [.. , .. , ..] are the rows.
>>
>> I am interested into getting the "row index" of all the matrix rows
>> where a certain number occurs.
>> For example for 9 I should get 2 and 3 (starting from 0).
>> For 10 I should get an error msg (item not found) and handle it.
>>
>> How to get the"row indexes" of found items?
>>
>> In practice I am looking for an equivalent to "list.index(x)" for the
>> case "lists of lists"

>
> Actually you are not list.index(x) gives you the index of the
> first occurence of the item.
>
> So what you seem to want is a list of indexes to the lists where
>
> Something like:
>
> x=9
>
> [idx for idx,row in enumerate(mat) if x in row]
>
> should do.
>
>>
>>
>> PS: this is just a simplified example, but I have actually to deal
>> with large matrices [~500000 * 4]

>
> This is something I'd consider either reordering your data (for example
> into dictionary) or look at scipy/numpy.
>
> Regards
> Tino
>

--
=========================
Alessandro Zivelonghi Ziller

Max-Planck-Institut für Plasmaphysik, Garching, DE
Middle-Age Fusion Engineer
===
http://www.tecnopolis.eu
skype: alexzive
" I'm sure that in 1985 plutonium is available in every corner drug
store, but in 1955 it's a little hard to come by"
Dr. D.Brown

Alessandro Zivelonghi, Mar 20, 2009
4. ### Chris RebertGuest

On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi
<> wrote:
> Many Thanks guys!
>
> and what if I need to look ONLY into the second and third columns,
> excluding the first item of each rows?
>
> for example if x = 3 I need to get Â [0] and not [0,1]

indices = [i for i, row in enumerate(mat) if 1<= i <= 2 and 3 in row[1:]]

Cheers,
Chris

--
I have a blog:
http://blog.rebertia.com

Chris Rebert, Mar 20, 2009
5. ### MRABGuest

Chris Rebert wrote:
> On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi
> <> wrote:
>> Many Thanks guys!
>>
>> and what if I need to look ONLY into the second and third columns,
>> excluding the first item of each rows?
>>
>> for example if x = 3 I need to get [0] and not [0,1]

>
> indices = [i for i, row in enumerate(mat) if 1<= i <= 2 and 3 in row[1:]]
>

If he wants to look in only the second and third columns, but still all
the rows, surely that's:

indices = [i for i, row in enumerate(mat) if x in row[1 : 3]]

MRAB, Mar 20, 2009
6. ### Alessandro ZivelonghiGuest

this seems to work. Thanks!
Alex

x= 3
indices = [i for i, row in enumerate(mat) if x in row[1:]]

2009/3/20 Chris Rebert <>:
> On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi
> <> wrote:
>> Many Thanks guys!
>>
>> and what if I need to look ONLY into the second and third columns,
>> excluding the first item of each rows?
>>
>> for example if x = 3 I need to get [0] and not [0,1]

>
> indices = [i for i, row in enumerate(mat) if 1<= i <= 2 and 3 in row[1:]]
>
> Cheers,
> Chris
>
> --
> I have a blog:
> http://blog.rebertia.com
>

--
=========================
Alessandro Zivelonghi Ziller

Max-Planck-Institut für Plasmaphysik, Garching, DE
Middle-Age Fusion Engineer
===
http://www.tecnopolis.eu
skype: alexzive
" I'm sure that in 1985 plutonium is available in every corner drug
store, but in 1955 it's a little hard to come by"
Dr. D.Brown

Alessandro Zivelonghi, Mar 20, 2009
7. ### Chris RebertGuest

On Fri, Mar 20, 2009 at 7:28 AM, MRAB <> wrote:
> Chris Rebert wrote:
>>
>> On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi
>> <> wrote:
>>>
>>> Many Thanks guys!
>>>
>>> and what if I need to look ONLY into the second and third columns,
>>> excluding the first item of each rows?
>>>
>>> for example if x = 3 I need to get Â [0] and not [0,1]

>>
>> indices = [i for i, row in enumerate(mat) if 1<= i <= 2 and 3 in row[1:]]
>>

> If he wants to look in only the second and third columns, but still all
> the rows, surely that's:
>
> Â  Â indices = [i for i, row in enumerate(mat) if x in row[1 : 3]]

The OP had his columns and rows mixed up in that last email, if you'll
notice what he gave as his intended output.

Cheers,
Chris

--
I have a blog:
http://blog.rebertia.com

Chris Rebert, Mar 20, 2009