# An ordering question

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

1. ### KottiyathGuest

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

2. ### MRABGuest

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?
>

>>> 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

3. ### MRABGuest

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?
>>

>
> >>> 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
4. ### Hrvoje NiksicGuest

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
5. ### Peter PearsonGuest

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
6. ### Chris RebertGuest

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
7. ### KottiyathGuest

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
8. ### Gabriel GenellinaGuest

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
>
> 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