a possibly foolish question about slices

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Jun 14 14:39:16 EDT 2002


Andrew Koenig <ark at research.att.com> writes:

>Suppose x is a string or list, and I want to refer to
>a subsequence of x with length n starting at position p.
>under the assumption that this subsequence is entirely

>To do so, I can use x[p:p+n] in all circumstances but one,
>namely when p < 0 and p == -n.  Is there a similarly concise
>expression that works even in this special case?

It frequently bugs me that you can't supply any number for b in <string>[a:b]
to indicate spanning to the end of the string.  In other words I'd love to
hear of a solution because I can't think of a way to do it either EXCEPT to
stop using the $%&^%$@@ zero based indexing and go back to using 1 as the
first item in a list (etc.) as nature intended (this is the way Icon works and
I always found it the nicest and most intuitive and frankly just plain
correct!) this way 0 is reserved for the last item (ie -2 -1 0 is a logical
progression).

So if the designers could fix this for version 2.3 I think we'll all be happy
:)

Actually, while you're at it why not also use Icon's <string>[x:+len] syntax
for the special case mentioned above (I've often wondered about that but can't
be bothered searching for previous discussions).

Eddie (the zeroeth amongst equals).



More information about the Python-list mailing list