On Thu, Jan 8, 2009 at 11:51 AM, culpritNr1 <span dir="ltr">&lt;<a href="mailto:ig2ar-saf1@yahoo.co.uk">ig2ar-saf1@yahoo.co.uk</a>&gt;</span> wrote:<br><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;">
<br>
Hi All,<br>
<br>
Say I have this nice list of lists:<br>
<br>
LoL = [[&#39;chrX&#39;, &#39;160944034&#39;, &#39;160944035&#39;, &#39;gnfX.145.788&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, &#39;161109992&#39;, &#39;161109993&#39;, &#39;rs13484104&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, &#39;161414112&#39;, &#39;161414113&#39;, &#39;rs13484105&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, &#39;161544071&#39;, &#39;161544072&#39;, &#39;rs13484106&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, &#39;162030736&#39;, &#39;162030737&#39;, &#39;gnfX.146.867&#39;, &#39;67.05&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, &#39;164171913&#39;, &#39;164171914&#39;, &#39;gnfX.148.995&#39;, &#39;70.45&#39;]]<br>
<br>
Now I want to cast the second and third &quot;columns&quot; from string to integer,<br>
like this<br>
<br>
LoL = [[&#39;chrX&#39;, 160944034, 160944035, &#39;gnfX.145.788&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, 161109992, 161109993, &#39;rs13484104&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, 161414112, 161414113, &#39;rs13484105&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, 161544071, 161544072, &#39;rs13484106&#39;, &#39;63.60&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, 162030736, 162030737, &#39;gnfX.146.867&#39;, &#39;67.05&#39;],<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&#39;chrX&#39;, 164171913, 164171914, &#39;gnfX.148.995&#39;, &#39;70.45&#39;]]<br>
<br>
Is there any elegant way to do this? I can&#39;t assume that all lines will have<br>
the same number of elements.<br>
</blockquote><div><br>Maybe not the most elegant way, but here&#39;s a quickie:<br>print(LoL)<br>for lstA in LoL:<br>&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lstA[1] = int(lstA[1])<br>&nbsp;&nbsp;&nbsp; except:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass<br>&nbsp;&nbsp;&nbsp; try:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lstA[2] = int(lstA[2])<br>
&nbsp;&nbsp;&nbsp; except:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pass<br>print(LoL) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;</div></div>I put them in separate try/excepts in case the first one&#39;s not an integer but the second one is.&nbsp; <br><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a><br>