locate items in matrix (index of lists of lists)

Alessandro Zivelonghi zasaconsulting at gmail.com
Fri Mar 20 07:34:31 EDT 2009


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 <tino at wildenhain.de>:
> 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
> 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



More information about the Python-list mailing list