[Tutor] which is better solution of the question

Alan Gauld alan.gauld at btinternet.com
Tue Jun 16 16:32:41 CEST 2009


"Abhishek Tiwari" <tiwariabhishekin at gmail.com> wrote

> *Ans. 1*
>
> values, items = list(zip(*sorted(zip(values,items), reverse=True)))

Personally I find that just a bit too dense so I'd break it out to:

s = sorted(zip(values,items), reverse=True)
values = [v for v,i in s]
items =  [i for v,i in s]

> *Ans. 2*
> new_values = sorted(values, reverse=True)
> new_items = [items[x] for x in map(values.index,new_values)]

I prefer this to the one-liner but don't like the map inside the 
comprehension.

But as the old Irish saying goes:
"If I was going there I wouldn't start from here!"

>From a purely readability point of view, and if I had any control
over the data formats I'd personally convert it to a dictionary of
value,item pairs and not bother with two lists. Sorting then
becomes a unified operation.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list