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 'convert'. Sorry, my badness.
Thanks for pointing that out.

culpritNr1





Kent Johnson wrote:
> 
> 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
> Python are you using?
> 
> Kent
> 
> PS Please subscribe to the list
> ___
> Tutor maillist  -  Tutor@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-tp21359600p21377706.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
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 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 was a pain in the butt, trapping the exception and doing
> "isinstance" checks against it.  How did I miss this?

wow, you were really going out of your way. i believe the docs have
some examples but cannot confirm this.

> Learn something new every day, no?

even i do.

oh, here's something else for the OP...

i forgot to mention there is a useful string method called isdigit()
that you can use on the string in question. it returns True if all the
chars in the string are digits, meaning that it's likely safe to call
int() on it.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
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
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], [7, 8, 9]]
> [[1, 8, 3], [4, 125, 6], [7, 512, 9]]
> 
> Now, as I said in my original post, I can't assume that all inner-lists will
> have the same number of elements. Common data looks like this
> 
> LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'],
>  ['chrX', '161109992', '161109993', 'rs13484104', '63.60'],
>  ['chrX', '161414112', '161414113', 'undetermined'],
>  ['chrX', '161544071', '161544072', 'rs13484106', '63.60',
> 'RNA-BP', 'PLoS Biol 6(10): e255'],
>  ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'],
>  ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']]
> 
> Do you still think that this problem can be elegantly tackled by list
> comprehensions?

The straightforward method is indeed to to loop with neccsary checks/trials 
embedded in loop. Now,
there imo is more elegant method which is to define a custom int that 
integrates such tests. And
then to call this custom conversion instead of built-in int. This lets open the 
possibility of a
list comp expression. Below an example:

def possible_cube(val):
try:
return val ** 3
except:
return val
lol = [[1,2,'x'],[4,'y',6],['z',8,9]]
new_lol = [[possible_cube(val) for val in l] for l in lol]
print lol
print new_lol
==>
[[1, 2, 'x'], [4, 'y', 6], ['z', 8, 9]]
[[1, 8, 'x'], [64, 'y', 216], ['z', 512, 729]]

This may be longer because there is a custom function call in each loop. Now, 
it's a question of
taste and priority.

denis


--
la vida e estranya
___
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 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
Python are you using?

Kent

PS Please subscribe to the list
___
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 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 '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  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@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>> The University of Dundee is a registered Scottish charity, No: SC015096
>>
> 
> ___
> Tutor maillist  -  Tutor@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.

___
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 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 post, I can't assume that all inner-lists will
have the same number of elements. Common data looks like this

LoL = [['chrX', '160944034', '160944035', 'gnfX.145.788', '63.60'],
 ['chrX', '161109992', '161109993', 'rs13484104', '63.60'],
 ['chrX', '161414112', '161414113', 'undetermined'],
 ['chrX', '161544071', '161544072', 'rs13484106', '63.60',
'RNA-BP', 'PLoS Biol 6(10): e255'],
 ['chrX', '162030736', '162030737', 'gnfX.146.867', '67.05'],
 ['chrX', '164171913', '164171914', 'gnfX.148.995', '70.45']]

Do you still think that this problem can be elegantly tackled by list
comprehensions?

Thank you,

culpritNr1






spir wrote:
> 
> 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', '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.
> 
> In you do not need checking whether specific items are "castable" (i.e.
> you really know what's in
> the list) you may use such a pattern:
> 
> 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]]
> 
> denis
>> Thank you,
>> 
>> Your Culprit
>> 
>> 
> 
> 
> --
> la vida e estranya
> ___
> Tutor maillist  -  Tutor@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-tp21359600p21375207.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
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 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)
>
>  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  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@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
> The University of Dundee is a registered Scottish charity, No: SC015096
>
___
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 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 comprehension operation. I
> wondered if the beauty of python could reach that far.


already given such a solution -- denis

> 
> I thank you all for your comments,
> 
> culpritNr1
> 
--
la vida e estranya
___
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 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 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  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@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> The University of Dundee is a registered Scottish charity, No: SC015096
> ___
> Tutor maillist  -  Tutor@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.

___
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 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 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  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@python.org
http://mail.python.org/mailman/listinfo/tutor

The University of Dundee is a registered Scottish charity, No: SC015096
___
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-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 tuple of exception types - that's
why I said it was a pain in the butt, trapping the exception and doing
"isinstance" checks against it.  How did I miss this?

Learn something new every day, no?
-- 
www.fsrtechnologies.com
___
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-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 = [['chrX', 160944034, 160944035, 'gnfX.145.788', '63.60'],
['chrX', 161109992, 161109993, 'rs13484104', '63.60'],


Being picky that's not really casting that's converting.
Casting is where you take the same set of binary data and treat it
as if it were a different type but without changing the binary.
Conversion is where you actually change the data into a new
representation. Conversion is relatively easy in Python, casting
is a bit more tricky.

Alan G. 



___
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-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: IndexError and ValueError.  I assumed 
> that if
> either one occurred, we would want to leave that particular list member (if
> it exists) as-is (hence the pass).
>:
> - catch the exception, test to see whether it's an IndexError or a
> ValueError, and if not then do something;
>:

as opposed to the above...

except (IndexError, ValueError):
pass

...is much more acceptable. there are 2 general conventions:

- catch errors explicitly (as to not mask the others) and pass/ignore
- catch 'em all (except Exception) and do *some*thing (not pass)

both of those cases shows that you did your due diligence and that
you're just not blindly throwing things away.


> Generally, if there are two or more "acceptable" errors that you can foresee,
> but you still want to catch any others, what's an elegant, readable,  and
> computationally-cheap way to do it?  Maybe a dictionary of exceptions...?

A tuple of exceptions works, just like what we did above, and more,
i.e., (IndexError, ValueError, TypeError, KeyError...

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
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-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
> everything!!
>
> if you are guaranteed that the strings all contain valid integers,
> then you don't have to worry about problems calling int().
>

OP said we couldn't be sure that all rows would have the same number of
elements; I'm afraid I extrapolated that we also might not be sure they were
all integers (or, I should say, strings representing integers...), so there
are two potential error types: IndexError and ValueError.  I assumed that if
either one occurred, we would want to leave that particular list member (if
it exists) as-is (hence the pass).

I see three ways around this:
- test each list for length, and each member for integer-ness, before
casting anything;
- catch the exception, test to see whether it's an IndexError or a
ValueError, and if not then do something;
- print/log every exception.

The first one violates "It's better to ask forgiveness than to ask
permission" (and it's slow), the second one's a pain in the butt; the third
one gets slow if LoL is large and has many non-standard members.  Is there a
fourth way?

Actually, I'm asking because I've run into similar situations and been
dissatisfied with my own solution.  Generally, if there are two or more
"acceptable" errors that you can foresee, but you still want to catch any
others, what's an elegant, readable,  and computationally-cheap way to do
it?  Maybe a dictionary of exceptions...?

-- 
www.fsrtechnologies.com
___
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-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 can't assume that all lines will
>> have the same number of elements.

the easiest way to do it is using int(), as in the below.


> for lstA in LoL:
> try:
> lstA[1] = int(lstA[1])
> except:
> pass
> try:
> lstA[2] = int(lstA[2])
> except:
> pass

it's definitely a *good* idea to check and make sure the numbers are
legit, meaning that int() won't throw an exception, but it is a *bad*
idea to have any code anywhere that looks like:

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!!

if you are guaranteed that the strings all contain valid integers,
then you don't have to worry about problems calling int().

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
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-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', '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.

In you do not need checking whether specific items are "castable" (i.e. you 
really know what's in
the list) you may use such a pattern:

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]]

denis
> Thank you,
> 
> Your Culprit
> 
> 


--
la vida e estranya
___
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-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', '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.
>

Maybe not the most elegant way, but here's a quickie:
print(LoL)
for lstA in LoL:
try:
lstA[1] = int(lstA[1])
except:
pass
try:
lstA[2] = int(lstA[2])
except:
pass
print(LoL)

I put them in separate try/excepts in case the first one's not an integer
but the second one is.

-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor