An ordering question

Discussion in 'Python' started by Kottiyath, Mar 13, 2009.

  1. Kottiyath

    Kottiyath Guest

    Hi,
    I have 2 lists
    a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    b = [2, 4, 1, 3]

    Now, I want to order _a_ (a[1]) based on _b_.
    i.e. the second element in tuple should be the same as b.
    i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]

    I did the same as follows:
    >>> l = len(a) * [None]
    >>> for (k, v) in a:

    .... for i, e in enumerate(b):
    .... if e == v:
    .... l = (k, v)

    This works, but the code -for python- looks very kludgy.
    I thought for ~2 hours to see whether I can do it in a line or 2,
    but I cannot seem to find a mechanism.
    Can someone help me out?
    Kottiyath, Mar 13, 2009
    #1
    1. Advertising

  2. Kottiyath

    MRAB Guest

    Kottiyath wrote:
    > Hi,
    > I have 2 lists
    > a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    > b = [2, 4, 1, 3]
    >
    > Now, I want to order _a_ (a[1]) based on _b_.
    > i.e. the second element in tuple should be the same as b.
    > i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]
    >
    > I did the same as follows:
    >>>> l = len(a) * [None]
    >>>> for (k, v) in a:

    > ... for i, e in enumerate(b):
    > ... if e == v:
    > ... l = (k, v)
    >
    > This works, but the code -for python- looks very kludgy.
    > I thought for ~2 hours to see whether I can do it in a line or 2,
    > but I cannot seem to find a mechanism.
    > Can someone help me out?
    >

    How about:

    >>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    >>> b = [2, 4, 1, 3]
    >>> d = dict((v, k) for k, v in a)
    >>> c = [(d, s) for s in b]
    >>> c

    [(3, 2), (2, 4), (4, 1), (7, 3)]

    Understanding how it works is left as an exercise for the reader. :)
    MRAB, Mar 13, 2009
    #2
    1. Advertising

  3. Kottiyath

    MRAB Guest

    MRAB wrote:
    > Kottiyath wrote:
    >> Hi,
    >> I have 2 lists
    >> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    >> b = [2, 4, 1, 3]
    >>
    >> Now, I want to order _a_ (a[1]) based on _b_.
    >> i.e. the second element in tuple should be the same as b.
    >> i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]
    >>
    >> I did the same as follows:
    >>>>> l = len(a) * [None]
    >>>>> for (k, v) in a:

    >> ... for i, e in enumerate(b):
    >> ... if e == v:
    >> ... l = (k, v)
    >>
    >> This works, but the code -for python- looks very kludgy.
    >> I thought for ~2 hours to see whether I can do it in a line or 2,
    >> but I cannot seem to find a mechanism.
    >> Can someone help me out?
    >>

    > How about:
    >
    > >>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    > >>> b = [2, 4, 1, 3]
    > >>> d = dict((v, k) for k, v in a)
    > >>> c = [(d, s) for s in b]
    > >>> c

    > [(3, 2), (2, 4), (4, 1), (7, 3)]
    >
    > Understanding how it works is left as an exercise for the reader. :)
    >

    Actually, a more general solution is:

    >>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    >>> b = [2, 4, 1, 3]
    >>> d = dict((t[1], t) for t in a)
    >>> c = [d for s in b]
    >>> c

    [(3, 2), (2, 4), (4, 1), (7, 3)]
    MRAB, Mar 13, 2009
    #3
  4. Kottiyath <> writes:

    > Hi,
    > I have 2 lists
    > a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    > b = [2, 4, 1, 3]
    >
    > Now, I want to order _a_ (a[1]) based on _b_.
    > i.e. the second element in tuple should be the same as b.
    > i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]

    [...]
    > whether I can do it in a line or 2,


    a.sort(key=lambda (x, y): b[y - 1], reverse=True)
    Hrvoje Niksic, Mar 13, 2009
    #4
  5. Parameter sublists [was: An ordering question]

    On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic <> wrote:
    [snip]
    > a.sort(key=lambda (x, y): b[y - 1], reverse=True)


    Huh? I had no idea one could do this:

    >>> def g( ( ( x, y ), z ) ):

    .... return y
    ....
    >>> g( ((1,2),3) )

    2

    What should I have read to learn that trick?

    --
    To email me, substitute nowhere->spamcop, invalid->net.
    Peter Pearson, Mar 14, 2009
    #5
  6. Kottiyath

    Chris Rebert Guest

    Re: Parameter sublists [was: An ordering question]

    On Fri, Mar 13, 2009 at 5:30 PM, Peter Pearson <> wrote:
    > On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic <> wrote:
    > [snip]
    >> a.sort(key=lambda (x, y): b[y - 1], reverse=True)

    >
    > Huh?  I had no idea one could do this:
    >
    >>>> def g( ( ( x, y ), z ) ):

    > ...   return y
    > ...
    >>>> g( ((1,2),3) )

    > 2
    >
    > What should I have read to learn that trick?


    Don't bother. It's been excised in Python 3.0.

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
    Chris Rebert, Mar 14, 2009
    #6
  7. Kottiyath

    Kottiyath Guest

    Re: Parameter sublists [was: An ordering question]

    On Mar 14, 5:39 am, Chris Rebert <> wrote:
    > On Fri, Mar 13, 2009 at 5:30 PM, Peter Pearson <> wrote:
    > > On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic <> wrote:
    > > [snip]
    > >> a.sort(key=lambda (x, y): b[y - 1], reverse=True)

    >
    > > Huh?  I had no idea one could do this:

    >
    > >>>> def g( ( ( x, y ), z ) ):

    > > ...   return y
    > > ...
    > >>>> g( ((1,2),3) )

    > > 2

    >
    > > What should I have read to learn that trick?

    >
    > Don't bother. It's been excised in Python 3.0.
    >
    > Cheers,
    > Chris
    >
    > --
    > I have a blog:http://blog.rebertia.com


    Thank you very much.
    These many solutions ?
    I think programming is not my forte :) :)
    Kottiyath, Mar 14, 2009
    #7
  8. En Fri, 13 Mar 2009 17:33:51 -0200, Hrvoje Niksic <>
    escribió:
    > "andrew cooke" <> writes:
    >> Hrvoje Niksic wrote:
    >>> Kottiyath <> writes:
    >>>
    >>>> I have 2 lists
    >>>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
    >>>> b = [2, 4, 1, 3]
    >>>> Now, I want to order _a_ (a[1]) based on _b_.
    >>>> i.e. the second element in tuple should be the same as b.
    >>>> i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]
    >>>
    >>> a.sort(key=lambda (x, y): b[y - 1], reverse=True)

    >>
    >> that's hilarious! i have no idea how it works

    >
    > Glad to have made someone laugh, but it in fact wasn't meant to. :)
    > It was a combination of misunderstanding the question *and* trying out
    > the answer.
    >
    > I understood that the second element of each tuple should be sorted
    > according to the corresponding element in b. This part is partly
    > correct, but I took corresponding to mean that it should be sorted by
    > the value of b[x], x being the second element of each tuple. Since
    > those elements appear to be 1-based, I added -1. This produced a list
    > in exactly the reverse order, so I added reverse=True.


    This shows how important is to provide a good, unambiguous example in any
    specification. Not just a "correct" example, but one that also shows how
    things are not to be done. In case of doubt, people always look at the
    examples for clarification.

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 14, 2009
    #8
    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. nospam
    Replies:
    13
    Views:
    612
    Guinness Mann
    Oct 7, 2003
  2. Craig Douglas

    Ordering FileInfo[] by date

    Craig Douglas, Jan 30, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    353
    Craig Douglas
    Jan 30, 2004
  3. nbigaouette

    Z-Ordering (Morton ordering) question

    nbigaouette, Nov 5, 2009, in forum: C Programming
    Replies:
    2
    Views:
    2,147
  4. Aryk Grosz
    Replies:
    3
    Views:
    108
    Aryk Grosz
    Jun 27, 2007
  5. John

    question about hash ordering

    John, Oct 2, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    87
    Tad McClellan
    Oct 2, 2003
Loading...

Share This Page