<br><br><div class="gmail_quote">On Tue, Jun 16, 2009 at 8:52 AM, Abhishek Tiwari <span dir="ltr">&lt;<a href="mailto:tiwariabhishekin@gmail.com">tiwariabhishekin@gmail.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;">

<div class="gmail_quote">
<div><font color="#990000"><b>Question :</b></font></div>
<div>The first list contains some items, and the second list contains their value (higher is better).</div>
<div> </div>
<div>items = [apple, car, town, phone] </div>
<div>values = [5, 2, 7, 1] </div>
<div> </div>
<div>Show how to sort the &#39;items&#39; list based on the &#39;values&#39; list so that you end up with the following two lists:</div>
<div> </div>
<div>items = [town, apple, car, phone] </div>
<div>values = [7, 5, 2, 1] </div>
<div> </div>
<div><font color="#000099"><b>Ans. 1</b></font> </div>
<div> </div>
<div>values, items = list(zip(*sorted(zip(values,items), reverse=True)))</div>
<div> </div>
<div><font color="#000099"><b>Ans. 2</b></font> </div>
<div>new_values = sorted(values, reverse=True)</div>
<div>new_items = [items[x] for x in map(values.index,new_values)]</div>
<div> </div>
</div></blockquote><div><br>I would use a dict to store the values: {1:&#39;phone&#39;, 5:&#39;apple&#39;, 2:&#39;car&#39;, 7:&#39;town&#39;} - then you just use sorted(mydict.keys(), reverse=True) to access them in a big-endian (most important) first, or without reverse if I wanted the least important value. This assumes that you can&#39;t have two values of the same weight, of course. <br>

<br>If you&#39;re looking for speed, I&#39;d look at timeit: <a href="http://docs.python.org/library/timeit.html">http://docs.python.org/library/timeit.html</a><br><br>I don&#39;t know which is considered more pythonic, though, so I&#39;m afraid I can&#39;t be of much more help than that.<br>

<br>HTH,<br>Wayne<br></div></div><br>