Reoedering indexes in list of list

Discussion in 'Python' started by Toto, Sep 28, 2010.

  1. Toto

    Toto Guest

    Hello,

    I have a list of list
    assume myList[x][y] is integer
    I would like to create an alias to that list which I could call this
    way:
    alias[y][x] returns myList[x][y]

    how can I do that ? (python 2.6)

    (I have a feeling I should use 'property' ;)

    Thanks,
    --
    Toto, Sep 28, 2010
    #1
    1. Advertising

  2. Toto

    Chris Rebert Guest

    On Tue, Sep 28, 2010 at 11:55 AM, Toto <> wrote:
    > Hello,
    >
    > I have a list of list
    > assume myList[x][y] is integer
    > I would like to create an alias to that list which I could call this
    > way:
    > alias[y][x] returns myList[x][y]


    If your "alias" can be read-only:
    alias = zip(*myList)

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Sep 28, 2010
    #2
    1. Advertising

  3. Toto

    Toto Guest


    > If your "alias" can be read-only:
    > alias = zip(*myList)




    a=[['00','01'],['10','11']]
    l=zip(*a)
    print(l)

    returns... [('00', '10'), ('01', '11')]

    IS NOT AT ALL WHAT I WANT ;-)

    What I want is

    print a[1][0]
    '10'
    but print l[1][0]
    '01'

    notice the indexes of the list l are inverted...
    Toto, Sep 28, 2010
    #3
  4. Toto

    Toto Guest

    again I want:

    alias[y][x] returns myList[x][y]

    > print a[1][0]
    > '10'
    > but print l[1][0]
    > '01'
    >
    > notice the indexes  of the list l are inverted...
    Toto, Sep 28, 2010
    #4
  5. Toto

    Toto Guest

    heu.... the zip trick actually works... my mistake!
    Toto, Sep 28, 2010
    #5
  6. Toto <> writes:

    >> If your "alias" can be read-only:
    >> alias = zip(*myList)

    >
    >
    >
    > a=[['00','01'],['10','11']]
    > l=zip(*a)
    > print(l)
    >
    > returns... [('00', '10'), ('01', '11')]
    >
    > IS NOT AT ALL WHAT I WANT ;-)
    >
    > What I want is
    >
    > print a[1][0]
    > '10'
    > but print l[1][0]
    > '01'
    >
    > notice the indexes of the list l are inverted...


    Ahem...

    >>> a = [['00', '01'], ['10', '11']]
    >>> l = zip(*a)
    >>> a[1][0]

    '10'
    >>> l[1][0]

    '01'

    Looks *exactly* like what you want to me. It doesn't take that long to
    check.

    --
    Arnaud
    Arnaud Delobelle, Sep 28, 2010
    #6
  7. On Tue, 28 Sep 2010 11:55:18 -0700 (PDT) Toto <>
    wrote:

    > Hello,
    >
    > I have a list of list
    > assume myList[x][y] is integer
    > I would like to create an alias to that list which I could call this
    > way:
    > alias[y][x] returns myList[x][y]
    >
    > how can I do that ? (python 2.6)
    >
    > (I have a feeling I should use 'property' ;)
    >

    The zip thing certainly seems nice. But I can never get my head around
    zip quick enough to not be confused by it. So my initial idea would
    have been a function:

    def reverse_indices(lst, x, y):
    return lst[y][x]

    (or, possibly, a subclass of list with this as an additional method.)

    Granted, the invocation is different from what you want, but I don't
    know enough about your problem to judge whether this is an issue.

    /W

    --
    INVALID? DE!
    Andreas Waldenburger, Sep 29, 2010
    #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. William \(Bill\) Vaughn

    Re: non-unique indexes / dataview

    William \(Bill\) Vaughn, Jul 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    1,732
    jai hanuman
    Jul 17, 2003
  2. valentin tihomirov

    negative indexes

    valentin tihomirov, Jan 6, 2004, in forum: VHDL
    Replies:
    2
    Views:
    516
    valentin tihomirov
    Jan 6, 2004
  3. Hubble

    Auto allocation of Indexes

    Hubble, Sep 6, 2005, in forum: VHDL
    Replies:
    8
    Views:
    571
    Charles, SAG
    Sep 8, 2005
  4. Santiago  Romero
    Replies:
    10
    Views:
    521
    Peter Otten
    Jan 21, 2008
  5. Replies:
    5
    Views:
    106
Loading...

Share This Page