- **Python**
(*http://www.velocityreviews.com/forums/f43-python.html*)

- - **locate items in matrix (index of lists of lists)**
(*http://www.velocityreviews.com/forums/t676497-locate-items-in-matrix-index-of-lists-of-lists.html*)

locate items in matrix (index of lists of lists)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] |

Re: locate items in matrix (index of lists of lists)On Fri, Mar 20, 2009 at 3:50 AM, Alexzive <zasaconsulting@gmail.com> 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 using numpy (google it). Cheers, Chris -- I have a blog: http://blog.rebertia.com |

Re: locate items in matrix (index of lists of lists)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@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 |

Re: locate items in matrix (index of lists of lists)On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi
<zasaconsulting@gmail.com> 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 |

Re: locate items in matrix (index of lists of lists)Chris Rebert wrote:
> On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi > <zasaconsulting@gmail.com> 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]] |

Re: locate items in matrix (index of lists of lists)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 <clp2@rebertia.com>: > On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi > <zasaconsulting@gmail.com> 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 |

Re: locate items in matrix (index of lists of lists)On Fri, Mar 20, 2009 at 7:28 AM, MRAB <google@mrabarnett.plus.com> wrote:
> Chris Rebert wrote: >> >> On Fri, Mar 20, 2009 at 4:34 AM, Alessandro Zivelonghi >> <zasaconsulting@gmail.com> 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 |

All times are GMT. The time now is 11:40 PM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.