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