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

culpritNr1 ig2ar-saf1 at yahoo.co.uk
Fri Jan 9 17:23:04 CET 2009


Thanks Daniel,

That is EXACTLY what I was looking for.

Actually, int() does not really work but this does:

[ [line[0], eval(line[1]), eval(line[2])] + line[3:] for line in LoL]

Again, thanks.

culpritNr1




Daniel Sarmiento-2 wrote:
> 
> I am not an expert and don't know if this is considered 'elegant', but
> this
> is what I would try
> 
> conv = [[j[0], int(j[1]), int(j[2])] + j[3:] for j in LoL]
> 
> 
>> 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-tp21359600p21375430.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list