[Python-ideas] [Python-Dev] Inclusive Range

Antoon Pardon Antoon.Pardon at rece.vub.ac.be
Tue Oct 12 10:14:21 CEST 2010

On Mon, Oct 11, 2010 at 05:35:21AM -0700, Ethan Furman wrote:
> Antoon Pardon wrote:
> >On Sat, Oct 09, 2010 at 01:37:03AM +0000, Steven D'Aprano wrote:
> >
> >>On Fri, 08 Oct 2010 15:53:17 -0400, Jed Smith wrote:
> >>
> >
> >I stand by that claim. I think it was fairly obvious that what I meant
> >was that it was impossible to give such a numeric value that would work
> >with arbitrary L.
> >
> >if L2 == list(reversed(L1)) and a and b are in the range 1 < x <= len(L),
> >we have the following invariant.
> >
> >  L1[a:b] == L2[b-1:a-1:-1]
> Are you sure?

I'm sorry, I was not careful enough in writing this down. It should be
that the invariant holds for 1 <= x < len

>>> a=3
>>> b=7
>>> L1=['e', 'g', 'h', 'k', 'o', 'p', 'r', 't', 'x', 'z']
>>> L2 = list(reversed(L1))
>>> L1
['e', 'g', 'h', 'k', 'o', 'p', 'r', 't', 'x', 'z']
>>> L2
['z', 'x', 't', 'r', 'p', 'o', 'k', 'h', 'g', 'e']
>>> L1[a:b]
['k', 'o', 'p', 'r']
>>> L2[b-1:a-1:-1]
['k', 'o', 'p', 'r']

> >However this no longer works if either nr is 0.
> In which case it's not an invariant, is it?

The point being that the breaking of the invariant at that point 
is IMO a sign of bad design. It makes it difficult to use the reversed
slice in a general way.

More specifically, if you have a function that produces an a1 and b1,
that in combination with a list, always gives you the desired slice
by writing L[a1:b1]. There is no straigtforward way to transform this
a1 and b1 into a2 and b2, so that L[a2:b2:-1] is the reversed of L[a1:b1]

Antoon Pardon

More information about the Python-list mailing list