<div dir="ltr"><br><br><div class="gmail_quote">2008/8/20 Ron Brennan <span dir="ltr"><<a href="mailto:brennan.ron@gmail.com">brennan.ron@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div dir="ltr"><div><br clear="all"> </div>
<div>Hello,</div>
<div> </div>
<div>I am trying to parse a log file. I want to sort based on the second element the list that is in the file.</div>
<div> </div>
<div>What is the best way to do this? The sort is just on the line itself where I want to re-organize the lines based on the second element of the csv file</div>
<div> </div>
<div>Thanks,</div>
<div>Ron</div></div>
<br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><p>You may use the key parameter of sort() or sorted for this.</p>
<p>If your data is homogenous (i.e. all items have the subitem you want to sort on and all are comparable), maybe something like this would work:</p><p>>>> from operator import itemgetter<br>>>> nested_list = [[9, 0, 5], [19, -10, -3], [-9, 30, -5], [4, 8, 15]]<br>
>>> list_sorted_on_2nd_elem = sorted(nested_list, key=itemgetter(2))<br>>>> list_sorted_on_2nd_elem<br>[[-9, 30, -5], [19, -10, -3], [9, 0, 5], [4, 8, 15]]<br>>>> <br><br></p><p>2nd element (starting with the 0th one) - actually the 3rd one, otherwise the index in itemgetter() should be adjusted.</p>
<p>hth,</p><p> Vlasta</p><p><br></p></div>