Re: Creating slice notation from string

2009-09-03 Thread Bob van der Poel
On Sep 2, 8:52 pm, Steven D'Aprano wrote: > On Wed, 02 Sep 2009 17:32:09 -0700, Bob van der Poel wrote: > > > Actually, nither this or Jan's latest is working properly. I don't know > > if it's the slice() function or what (I'm using python 2.5). But: > > > x = [1,2,3,4,5] > > slice_string="2" > >

Re: Creating slice notation from string

2009-09-03 Thread Falcolas
On Sep 2, 3:55 pm, bvdp wrote: > I'm trying to NOT create a parser to do this and I'm sure that > it's easy if I could only see the light! > > Is it possible to take an arbitrary string in the form "1:2", "1", > ":-1", etc. and feed it to slice() and then apply the result to an > existing lis

Re: Creating slice notation from string

2009-09-02 Thread Paul McGuire
On Sep 2, 4:55 pm, bvdp wrote: > I'm trying to NOT create a parser to do this and I'm sure that > it's easy if I could only see the light! > Well, this is a nice puzzler, better than a sudoku. Maybe a quick parser with pyparsing will give you some guidance on how to do this without a parser

Re: Creating slice notation from string

2009-09-02 Thread Steven D'Aprano
On Wed, 02 Sep 2009 17:32:09 -0700, Bob van der Poel wrote: > Actually, nither this or Jan's latest is working properly. I don't know > if it's the slice() function or what (I'm using python 2.5). But: > > x = [1,2,3,4,5] > slice_string="2" > items = [int(n) if n else None for n in slice_string.s

Re: Creating slice notation from string

2009-09-02 Thread Ethan Furman
On Wed, 02 Sep 2009 17:36:36 -0700, Bob van der Poel wrote: On Sep 2, 4:27 pm, Ethan Furman wrote: Bob van der Poel wrote: >>For a one-liner: >>   x[slice(*map(int, x[1:-1].split(':')))] > Thanks. > Almost works :) > For s="[2]" and s="[1:2]" it's fine. But, if I have > s = "[:2]" t

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 4:27 pm, Ethan Furman wrote: > Bob van der Poel wrote: > > > > >>For a one-liner: > > >>   x[slice(*map(int, x[1:-1].split(':')))] > > > Thanks. > > > Almost works :) > > > For s="[2]" and s="[1:2]" it's fine. But, if I have > > > s = "[:2]" then I get: > > x[slice(*[int(i) for i in

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 5:16 pm, Steven D'Aprano wrote: > On Wed, 02 Sep 2009 16:41:34 -0700, Bob van der Poel wrote: > > > But, translating 1, 2 or 3 ints into a valid splice isn't quit that > > easy? I could figure each value, and convert them to either int or None > > (key is the None! From my previous try '

Re: Creating slice notation from string

2009-09-02 Thread Ethan Furman
Bob van der Poel wrote: For a one-liner: x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s="[2]" and s="[1:2]" it's fine. But, if I have s = "[:2]" then I get: x[slice(*[int(i) for i in s.strip("[]").split(":")])] Traceback (most recent call last): File "", lin

Re: Creating slice notation from string

2009-09-02 Thread Steven D'Aprano
On Wed, 02 Sep 2009 16:41:34 -0700, Bob van der Poel wrote: > But, translating 1, 2 or 3 ints into a valid splice isn't quit that > easy? I could figure each value, and convert them to either int or None > (key is the None! From my previous try '' doesn't work!) > > But, I still need three possib

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 4:43 pm, "Jan Kaliszewski" wrote: > 03-09-2009 o 00:55:10 Bob van der Poel wrote: > > > > >> For a one-liner: > > >>    x[slice(*map(int, x[1:-1].split(':')))] > > > Thanks. > > > Almost works :) > > > For s="[2]" and s="[1:2]" it's fine. But, if I have > > > s = "[:2]" then I get: > >

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 4:16 pm, "Rhodri James" wrote: > On Wed, 02 Sep 2009 23:57:48 +0100, Bob van der Poel   > wrote: > > > > >> Of course, you could also do something like this: > > >>      eval('x' + s) > >> or > >>      eval(str(x) + s) > > > Yes, I have user inputed 's'. So, if I can't get the generaliz

Re: Creating slice notation from string

2009-09-02 Thread Jan Kaliszewski
03-09-2009 o 00:55:10 Bob van der Poel wrote: For a one-liner:    x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s="[2]" and s="[1:2]" it's fine. But, if I have s = "[:2]" then I get: x[slice(*[int(i) for i in s.strip("[]").split(":")])] Traceback (most recent call

Re: Creating slice notation from string

2009-09-02 Thread Robert Kern
On 2009-09-02 17:55 PM, Bob van der Poel wrote: For a one-liner: x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s="[2]" and s="[1:2]" it's fine. But, if I have s = "[:2]" then I get: x[slice(*[int(i) for i in s.strip("[]").split(":")])] Traceback (most recent c

Re: Creating slice notation from string

2009-09-02 Thread Rhodri James
On Wed, 02 Sep 2009 23:57:48 +0100, Bob van der Poel wrote: Of course, you could also do something like this:      eval('x' + s) or      eval(str(x) + s) Yes, I have user inputed 's'. So, if I can't get the generalized list version from Robert working I'll have to use this. Speed is not

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
> Of course, you could also do something like this: > >      eval('x' + s) > or >      eval(str(x) + s) > Yes, I have user inputed 's'. So, if I can't get the generalized list version from Robert working I'll have to use this. Speed is not a big deal in this. As to malicious input, I could pretty

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
> For a one-liner: > >    x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s="[2]" and s="[1:2]" it's fine. But, if I have s = "[:2]" then I get: >>> x[slice(*[int(i) for i in s.strip("[]").split(":")])] Traceback (most recent call last): File "", line 1, in ValueError:

Re: Creating slice notation from string

2009-09-02 Thread Jan Kaliszewski
Erratum: eval(str(x) + s) -- but it's worse: less secure (e.g. if s could be user-typed) and most probably much more time-consuming (especially the latter). There should be *repr* instead of *str*. *j -- Jan Kaliszewski (zuo) -- http://mail.python.org/mailman/listinfo/python-list

Re: Creating slice notation from string

2009-09-02 Thread Jan Kaliszewski
03-09-2009 o 00:11:17 MRAB wrote: bvdp wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form "1:2", "1", ":-1", etc. and feed it to slice() and then apply the result to an exis

Re: Creating slice notation from string

2009-09-02 Thread Robert Kern
On 2009-09-02 16:55 PM, bvdp wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form "1:2", "1", ":-1", etc. and feed it to slice() and then apply the result to an existing list? F

Re: Creating slice notation from string

2009-09-02 Thread MRAB
bvdp wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form "1:2", "1", ":-1", etc. and feed it to slice() and then apply the result to an existing list? For example, I have a nor

Creating slice notation from string

2009-09-02 Thread bvdp
I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form "1:2", "1", ":-1", etc. and feed it to slice() and then apply the result to an existing list? For example, I have a normal python lis