locate items in matrix (index of lists of lists)

A

Alexzive

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]
 
C

Chris Rebert

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
using numpy (google it).

Cheers,
Chris
 
A

Alessandro Zivelonghi

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 said:
Alexzive said:
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
your item is contained.

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
 
C

Chris Rebert

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
 
M

MRAB

Chris said:
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]]
 
A

Alessandro Zivelonghi

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



--
=========================
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
 
C

Chris Rebert

Chris said:
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
 

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,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top