[Tutor] casting string to integer in a list of lists

culpritNr1 ig2ar-saf1 at yahoo.co.uk
Fri Jan 9 15:20:26 CET 2009


Hello Trias and all,

Glad to see that somebody recognized the BED genomic annotation format.

Your naive approach is probably the first thing that one could try. It is
sure to work. The problem is that your code becomes unnecessarily
long/cumbersome: every time I would have to use a particular element I would
have to cast it first.

So, an alternative idea is to modify the entire list of lists and get done
with once and for all. This could be done manually with a couple of FOR
loops. It would be easy and somebody already provided a solution early on
(thank you!).

Being a relative newcomer to python, my question was if there was an ELEGANT
way to do this casting, perhaps as a list comprehension operation. I
wondered if the beauty of python could reach that far.

I thank you all for your comments,

culpritNr1







trias wrote:
> 
> Hi Your,
> 
>  I work with genomic datasets as well and have recently only started
> working with python (so my advice is a bit naive)
> 
>  I would say although there may be different ways you can cast an integer
> or float type into your list of lists you may actually no need to do so
> with your starting file/list... ie:
> 
>  if for example you want to do stuff with the location chrX with start
> 160944034 and end 160944035
> 
>  you could: 
> for x in LoL:
> startstring=LoL[1]  #this would be '160944034'
> startint=int(startstring) #this would be 160944034
> 
> now if you also use a counter you can iterate and do all sort of
> calculations for different genomic locations.
> 
>  I use the csv module and then append into a numpy array that takes type
> float, so you could work with that as well,
> 
> cheers
>   
> 
> Dr Triantafyllos Gkikopoulos
>>>> culpritNr1 <ig2ar-saf1 at yahoo.co.uk> 01/08/09 8:42 PM >>>
> 
> Hi All,
> 
> Say I have this nice list of lists:
> 
> LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'],
>              ['chrX', '161109992', '161109993', 'rs13484104', '63.60'],
>              ['chrX', '161414112', '161414113', 'rs13484105', '63.60'],
>              ['chrX', '161544071', '161544072', 'rs13484106', '63.60'],
>              ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'],
>              ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']]
> 
> Now I want to cast the second and third "columns" from string to integer,
> like this
> 
> LoL = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'],
>              ['chrX', 161109992, 161109993, 'rs13484104', '63.60'],
>              ['chrX', 161414112, 161414113, 'rs13484105', '63.60'],
>              ['chrX', 161544071, 161544072, 'rs13484106', '63.60'],
>              ['chrX', 162030736, 162030737, 'gnfX.146.867', '67.05'],
>              ['chrX', 164171913, 164171914, 'gnfX.148.995', '70.45']]
> 
> Is there any elegant way to do this? I can't assume that all lines will
> have
> the same number of elements.
> 
> Thank you,
> 
> Your Culprit
> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21359600.html
> Sent from the Python - tutor mailing list archive at Nabble.com.
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> The University of Dundee is a registered Scottish charity, No: SC015096
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

-- 
View this message in context: http://www.nabble.com/casting-string-to-integer-in-a-list-of-lists-tp21359600p21373193.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list