Re: [Tutor] casting

2012-09-23 Thread Steven D'Aprano
On 24/09/12 00:44, Gina wrote: what is casting? Depends on the context. I presume you're talking about programming, not movie-making. Without knowing the context, it's hard to tell exactly what people mean when they say "cast", but as a rough translation, you won't be terribly far wrong if yo

[Tutor] casting

2012-09-23 Thread Gina
what is casting? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

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

2009-01-09 Thread culpritNr1
Hello Kent and All, Errata: int() does work. I think that in my test code a number such as '4.5' might have slipped in and then int() protested. eval() worked in all my attempts. So, thanks Daniel Sarmiento, your solution is correct as is. By the way, when I said 'cast' I really meant 'conver

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

2009-01-09 Thread wesley chun
>> A tuple of exceptions works, just like what we did above, and more, >> i.e., (IndexError, ValueError, TypeError, KeyError... >> > Thank you, thank you, thank you! I'm sure it's been staring me in the face, > but I never realized I could use a tuple of exception types - that's why I > said it wa

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

2009-01-09 Thread spir
Forgot a "detail" > def possible_cube(val): > try: > return val ** 3 > except TypeError: > return val denis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

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

2009-01-09 Thread spir
Le Fri, 9 Jan 2009 08:10:27 -0800 (PST), culpritNr1 a écrit : > > Hello Denis and All, > > Your solution does show elegance. To remind people, it's this one: > > lol = [[1,2,3],[4,5,6],[7,8,9]] > new_lol = [[a,b**3,c] for [a,b,c] in lol] > print lol > print new_lol > ==> > [[1, 2, 3], [4, 5, 6

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

2009-01-09 Thread Kent Johnson
On Fri, Jan 9, 2009 at 11:23 AM, culpritNr1 wrote: > 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] That's strange. What happened when you tried int() ? What version of Pytho

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

2009-01-09 Thread culpritNr1
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

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

2009-01-09 Thread culpritNr1
Hello Denis and All, Your solution does show elegance. To remind people, it's this one: lol = [[1,2,3],[4,5,6],[7,8,9]] new_lol = [[a,b**3,c] for [a,b,c] in lol] print lol print new_lol ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[1, 8, 3], [4, 125, 6], [7, 512, 9]] Now, as I said in my original pos

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

2009-01-09 Thread Daniel Sarmiento
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) > >

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

2009-01-09 Thread spir
Le Fri, 9 Jan 2009 06:20:26 -0800 (PST), culpritNr1 a écrit : > > Hello Trias and all, > > Glad to see that somebody recognized the BED genomic annotation format. > Being a relative newcomer to python, my question was if there was an ELEGANT > way to do this casting, perhaps as a list comprehe

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

2009-01-09 Thread culpritNr1
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 el

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

2009-01-09 Thread Triantafyllos Gkikopoulos
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 f

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

2009-01-08 Thread Marc Tompkins
On Thu, Jan 8, 2009 at 3:45 PM, wesley chun wrote: > A tuple of exceptions works, just like what we did above, and more, > i.e., (IndexError, ValueError, TypeError, KeyError... > > Thank you, thank you, thank you! I'm sure it's been staring me in the face, but I never realized I could use a tupl

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

2009-01-08 Thread Alan Gauld
"culpritNr1" wrote Say I have this nice list of lists: LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], ['chrX', '161109992', '161109993', 'rs13484104', '63.60'], Now I want to cast the second and third "columns" from string to integer, like this LoL = [['

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

2009-01-08 Thread wesley chun
>> except: >>pass >> >> try not to code these 2 lines in anything that you do because it will >> come back to haunt you when something is not working right but you >> can't find any errors. that's because this code masks and throws away >> everything!! > > there are two potential error types: I

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

2009-01-08 Thread Marc Tompkins
On Thu, Jan 8, 2009 at 2:21 PM, wesley chun wrote: > except: >pass > > try not to code these 2 lines in anything that you do because it will > come back to haunt you when something is not working right but you > can't find any errors. that's because this code masks and throws away > everythin

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

2009-01-08 Thread wesley chun
>> LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'], >>: >> 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'], >>: >> Is there any elegant way to do this? I ca

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

2009-01-08 Thread spir
Le Thu, 8 Jan 2009 11:51:01 -0800 (PST), culpritNr1 a écrit : > > 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

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

2009-01-08 Thread Marc Tompkins
On Thu, Jan 8, 2009 at 11:51 AM, culpritNr1 wrote: > > 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',

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

2009-01-08 Thread culpritNr1
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', '1615