locate items in matrix (index of lists of lists)

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

  1. Alexzive

    Alexzive Guest

    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
    #1
    1. Advertising

  2. Alexzive

    Chris Rebert Guest

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

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
    Chris Rebert, Mar 20, 2009
    #2
    1. Advertising

  3. 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
    > 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
    Alessandro Zivelonghi, Mar 20, 2009
    #3
  4. Alexzive

    Chris Rebert Guest

    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
    #4
  5. Alexzive

    MRAB Guest

    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
    #5
  6. 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
    #6
  7. Alexzive

    Chris Rebert Guest

    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
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,667
    Jonathan Bromley
    Jul 5, 2006
  2. Charlotte Henkle

    Counter for items in lists in lists?

    Charlotte Henkle, Sep 25, 2004, in forum: Python
    Replies:
    8
    Views:
    397
    Charlotte Henkle
    Sep 26, 2004
  3. ardief
    Replies:
    14
    Views:
    735
    Paddy
    Feb 3, 2007
  4. zaphod
    Replies:
    7
    Views:
    164
    Tad McClellan
    Jun 21, 2005
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    278
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page