On Thu, Jan 8, 2009 at 11:51 AM, culpritNr1 <span dir="ltr"><<a href="mailto:ig2ar-saf1@yahoo.co.uk">ig2ar-saf1@yahoo.co.uk</a>></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 = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'],<br>
['chrX', '161109992', '161109993', 'rs13484104', '63.60'],<br>
['chrX', '161414112', '161414113', 'rs13484105', '63.60'],<br>
['chrX', '161544071', '161544072', 'rs13484106', '63.60'],<br>
['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'],<br>
['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']]<br>
<br>
Now I want to cast the second and third "columns" from string to integer,<br>
like this<br>
<br>
LoL = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'],<br>
['chrX', 161109992, 161109993, 'rs13484104', '63.60'],<br>
['chrX', 161414112, 161414113, 'rs13484105', '63.60'],<br>
['chrX', 161544071, 161544072, 'rs13484106', '63.60'],<br>
['chrX', 162030736, 162030737, 'gnfX.146.867', '67.05'],<br>
['chrX', 164171913, 164171914, 'gnfX.148.995', '70.45']]<br>
<br>
Is there any elegant way to do this? I can'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's a quickie:<br>print(LoL)<br>for lstA in LoL:<br> try:<br> lstA[1] = int(lstA[1])<br> except:<br> pass<br> try:<br> lstA[2] = int(lstA[2])<br>
except:<br> pass<br>print(LoL) <br> </div></div>I put them in separate try/excepts in case the first one's not an integer but the second one is. <br><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a><br>