Re: An ordering question

2009-03-13 Thread Gabriel Genellina
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

Re: Parameter sublists [was: An ordering question]

2009-03-13 Thread Kottiyath
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( ( (

Re: Parameter sublists [was: An ordering question]

2009-03-13 Thread Chris Rebert
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

Parameter sublists [was: An ordering question]

2009-03-13 Thread Peter Pearson
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, substi

Re: An ordering question

2009-03-13 Thread John Posner
> If you don't want to build the intermediary dict, a > less efficient > version that runs in O(n^2): > > a.sort(key=lambda k: b.index(k[1])) > > Which is mostly similar to John's solution, but still > more efficient > because it only does a b.index call once per 'a' > item instead of twice > pe

Re: An ordering question

2009-03-13 Thread Hrvoje Niksic
"andrew cooke" writes: > Hrvoje Niksic wrote: >> 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

Re: An ordering question

2009-03-13 Thread andrew cooke
MRAB wrote: > >>> 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], s) for s in b] > >>> c > [(3, 2), (2, 4), (4, 1), (7, 3)] ah, that is more efficient than the suggestions i posted. andrew -- http://mail.python.org/mailman/

Re: An ordering question

2009-03-13 Thread andrew cooke
Hrvoje Niksic wrote: > 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),

Re: An ordering question

2009-03-13 Thread Hrvoje Niksic
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)] [...] > whe

Re: An ordering question

2009-03-13 Thread MRAB
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

Re: An ordering question

2009-03-13 Thread MRAB
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:

An ordering question

2009-03-13 Thread Kottiyath
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) * [N