<br><br><div class="gmail_quote">On Wed, Jun 17, 2009 at 12:01 AM, Jeff Rush <span dir="ltr">&lt;<a href="mailto:jeff@taupro.com">jeff@taupro.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Abhishek Tiwari wrote:<br>
&gt;<br>
&gt; *Ans. 1*<br>
<div class="im">&gt;<br>
&gt; values, items = list(zip(*sorted(zip(values,items), reverse=True)))<br>
&gt;<br>
&gt; *Ans. 2*<br>
&gt; new_values = sorted(values, reverse=True)<br>
&gt; new_items = [items[x] for x in map(values.index,new_values)]<br>
&gt;<br>
&gt; I would like to know which method is better and why?<br>
<br>
</div>The first one is better because the second one has a bug. ;-)<br>
<br>
Because of the way the second one uses values.index, it assumes there is<br>
a unique mapping between items and values.  When a duplicate integer<br>
appears in values, the second solution returns the wrong answer.</blockquote><div><br>Here is a 4th solution.<br><br> [item[1] for item in sorted(zip(values, items))][-1::-1]<br><br> If you are too lazy to write a timeit function, you can use my timeit<br>
 wrapper script at ASPN cookbook.<br><br> <a href="http://code.activestate.com/recipes/389767/">http://code.activestate.com/recipes/389767/</a><br><br>I timed all the solutions provided so far.<br><br>def f1(): list(zip(*sorted(zip(values,items), reverse=True)))<br>
<br>def f2():<br>    new_values = sorted(values, reverse=True); [items[x] for x map(values.index,new_values)]<br><br>def f3():<br>    [item[1] for item in sorted(zip(values, items))][-1::-1]<br><br>def f4():<br>    d = dict(zip(items, values))<br>
    new_items = sorted(items, key=d.__getitem__, reverse=True)<br> <br>Here are the times printed in order of f1...f4 for 10,000 passes.<br><br>[anand@localhost python]$ python listsort.py<br>6.97 usec/pass<br>6.95 usec/pass<br>
4.65 usec/pass<br>9.27 usec/pass<br></div><div><br>Don&#39;t use sorted(..., reverse=True), it is a killer in terms of time.<br>This is the reason why f4(...) does not work anywhere close to<br>what is expected out of a dict(...) solution and why f1(...) performs<br>
much below par.<br><br>For example, let us change f4(...) to not to use sorted(..., reverse=True)<br><br>def f4():<br>    d = dict(zip(items, values))<br>    new_items = sorted(items, key=d.__getitem__)[-1::-1]<br><br>The performance improves a bit,<br>
<br>[anand@localhost python]$ python listsort.py<br>7.06 usec/pass<br>7.04 usec/pass<br>4.61 usec/pass<br>9.04 usec/pass<br><br>To see the real effect of sorted(..., reverse=True), let us modify f3(...) to <br> use it instead of reversing the list at the end.<br>
<br><br>
def f3():<br>
    [item[1] for item in sorted(zip(values, items), reverse=True)]<br><br> [anand@localhost python]$ python listsort.py<br>7.19 usec/pass<br>7.13 usec/pass<br>6.39 usec/pass*<br>9.72 usec/pass<br><br>The function f3() slows down almost 1.3 times.<br>
<br>Don&#39;t use sorted(..., reverse=True). Instead reverse the list in place<br>by using reverse slicing of l[-1::-1], which is about 1.5 times faster.<br><br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<font color="#888888"><br>
-Jeff<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
BangPypers mailing list<br>
<a href="mailto:BangPypers@python.org">BangPypers@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/bangpypers" target="_blank">http://mail.python.org/mailman/listinfo/bangpypers</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>-Anand<br><br><br><br>