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
>> 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
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
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
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
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
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
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)
>
>
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
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
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
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
"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 = [['
>> 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
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
>> 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
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
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',
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
19 matches
Mail list logo