An ordering question

Hrvoje Niksic hniksic at xemacs.org
Fri Mar 13 13:56:30 EDT 2009


Kottiyath <n.kottiyath at gmail.com> 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)



More information about the Python-list mailing list