[BangPypers] which is better solution of the question

Jeff Rush jeff at taupro.com
Tue Jun 16 20:31:54 CEST 2009


Abhishek Tiwari wrote:
>  
> *Ans. 1*
>  
> values, items = list(zip(*sorted(zip(values,items), reverse=True)))
>  
> *Ans. 2*
> new_values = sorted(values, reverse=True)
> new_items = [items[x] for x in map(values.index,new_values)]
>  
> I would like to know which method is better and why?

The first one is better because the second one has a bug. ;-)

Because of the way the second one uses values.index, it assumes there is
a unique mapping between items and values.  When a duplicate integer
appears in values, the second solution returns the wrong answer.

-Jeff


More information about the BangPypers mailing list