Creating slice notation from string

B

Bob van der Poel

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.split(":")]
[slice(*items)]
[1, 2]

It's not clear what is input and what is output. I'm *guessing* that the
first four lines are input and the fifth is output.

By the way, nice catch for the "else None". But why are you wrapping the
call to slice() in a list in the fourth line?

I can't replicate your results. I get the expected results:
slice_string="2"
items = [int(n) if n else None for n in slice_string.split(":")]
[slice(*items)]

[slice(None, 2, None)]

exactly the same as:

slice(None, 2, None)

Testing this, I get the expected result:
x = [1,2,3,4,5]
x[slice(*items)]

[1, 2]

which is exactly the same if you do this:

[1, 2]
not the expected:

Why would you expect that? You can't get that result from a slice based
x[2::] [3, 4, 5]
x[:2:] [1, 2]
x[::2]

[1, 3, 5]

There is no slice containing *only* 2 which will give you the result you
are asking for. You would need to do this:

[3]

Perhaps what you are thinking of is *indexing*:

3

but notice that the argument to list.__getitem__ is an int, not a slice,
and the result is the item itself, not a list.

To get the behaviour you want, you need something more complicated:

def strToSlice(s):
    if ':' in s:
        items = [int(n) if n else None for n in s.split(':')]
    else:
        if s:
            n = int(s)
            items = [n, n+1]
        else:
            items = [None, None, None]
    return slice(*items)
Things like -1 don't work either.

They do for me:
slice_string="2:-2"
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
items = [int(n) if n else None for n in slice_string.split(":")]
x[ slice(*items) ] [3, 4, 5, 6, 7, 8]
x[2:-2]

[3, 4, 5, 6, 7, 8]
I'm really not sure what's going on, but I suspect it's the way that
slice() is getting filled when the slice string isn't a nice one with
each ":" present?

I think you're confused between __getitem__ with a slice argument and
__getitem__ with an int argument.

Yes, you're right ... I'm confused on what results I expect! Sorry
about that ... now that another day has passed it's all a bit more
clear.

I need to print out a number of these messages and write some test
code.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top