<div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Message: 8<br>
Date: Wed, 18 Jun 2008 19:30:05 -0500<br>
From: Keith Troell &lt;<a href="mailto:ktroell@mac.com">ktroell@mac.com</a>&gt;<br>
Subject: [Tutor] Newbie: Sorting lists of lists<br>
To: <a href="mailto:tutor@python.org">tutor@python.org</a><br>
Message-ID: &lt;<a href="mailto:2AAF54E8-95D9-4CB3-BD1E-CA2270E4B165@mac.com">2AAF54E8-95D9-4CB3-BD1E-CA2270E4B165@mac.com</a>&gt;<br>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed<br>
<br>
Let&#39;s say I have a list of lists l == [[1, 2, 3], [2, 3, 1], [3, 2,<br>
1], [1, 3, 2]]<br>
<br>
If I do a l.sort(), it sorts on the first element of each listed list:<br>
<br>
&nbsp;&gt;&gt;&gt; l.sort()<br>
&nbsp;&gt;&gt;&gt; l<br>
[[1, 2, 3], [1, 3, 2], [2, 3, 1], [3, 2, 1]]<br>
<br>
<br>
How can I sort on the second or third elements of the listed lists?</blockquote><div><br>I know, it&#39;s NOT beautiful code, but it seems work. If you have no better way, try this:<br><br>&quot;&quot;&quot;Sort on the second elements.&quot;&quot;&quot;<br>
def xchg12(l) :<br>&nbsp;&nbsp;&nbsp; for m in l:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m[0], m[1] = m[1], m[0]<br><br>l = [[1, 2, 3], [2, 3, 1], [3, 2,1], [1, 3, 2]]<br>print l<br>xchg12(l)<br>l.sort()<br>xchg12(l)<br>print l<br><br>Yuan (yet unlovely another newbie)<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>
<br>
Keith<br>
<br>
A++ G++ PKR+ !PEG- B++ TB ADB- M+++ CHOW++<br>
<a href="http://zbigniew.pyrzqxgl.com/bargegeek.html" target="_blank">http://zbigniew.pyrzqxgl.com/bargegeek.html</a><br>
<br>
<br>
<br>
<br>
------------------------------<br>
<br>
_______________________________________________<br>
Tutor maillist &nbsp;- &nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br>
<br>
End of Tutor Digest, Vol 52, Issue 51<br>
*************************************<br>
</blockquote></div><br><br clear="all"><br>-- <br>Fran Lebowitz &nbsp;- &quot;You&#39;re only has good as your last haircut.&quot;