Misleading description of [i:j:k] slicing?

R

Raoul Gough

As of Python 2.3, Section 2.2.6 (Sequence Types) describes slices
which have a specified step (to omit some indexes in beteween), using
the notation s[i:j:k]. The note about how this works says:

"The slice of s from i to j with step k is defined as the sequence
of items with index x = i + n*k such that 0 <= n < abs(i-j). [...]"

Seems to me that "0 <= n < abs(i-j)" is wrong, since the range of n
gets multiplied by k. I would suggest that it should be something
like:

"x = i + n, such that n is a multiple of k and 0 <= n < abs(i-j)"

or maybe better

"x = i + n*k, 0 <= n < ((j-i) / k)"

(Requiring j>i for positive k and j<i for negative k). I've tested the
implementation as follows:
x=[0,1,2,3,4,5,6,7,8,9]
print x[0:5:2] [0, 2, 4] # Would print [0, 2, 4, 6, 8] according to doc?
print x[2:5:-2]
[] # Would print [2, 0, 8] according to doc??

I was going to submit a bug, but then I realised that I might just be
mis-interpreting what the documentation says (or maybe it'd not even
supposed to be so precise a specification). Any comments or
confirmation on this?
 
M

Michael Hudson

Raoul Gough said:
As of Python 2.3, Section 2.2.6 (Sequence Types) describes slices
which have a specified step (to omit some indexes in beteween), using
the notation s[i:j:k]. The note about how this works says:

"The slice of s from i to j with step k is defined as the sequence
of items with index x = i + n*k such that 0 <= n < abs(i-j). [...]"

Seems to me that "0 <= n < abs(i-j)" is wrong, since the range of n
gets multiplied by k.

How about "0 <= n < abs(k*(i-j))"? But you're right, what's there is
a bit wrong. It's surprisingly hard to get this written down well.
The idea's not that hard, but a terse explanation is surprisingly
hard (when you start omitting values it gets even more fun!).

Please submit a patch (assign it to me if you like -- the above
passage is my fault).

Cheers,
mwh
 
R

Raoul Gough

Christos "TZOTZIOY" Georgiou said:
"The slice of s from i to j with step k is defined as the sequence
of items with index x = i + n*k such that 0 <= n < abs(i-j). [...]"

Basically, it's 0 <= n < abs(i-j)//k, right?

I didn't know about // (silly me) but it looks like a good way to
go. Your alternative formulation still doesn't give the right
behaviour for negative values of k. e.g.

s[4:0:-1]. i.e. i=4, j=0, k=-1 so 0 <= n < -4 ??

or, indeed, for positive k and j<i (should return an empty sequence).
 
R

Raoul Gough

Michael Hudson said:
How about "0 <= n < abs(k*(i-j))"? But you're right, what's there is
a bit wrong. It's surprisingly hard to get this written down well.
The idea's not that hard, but a terse explanation is surprisingly
hard (when you start omitting values it gets even more fun!).

Please submit a patch (assign it to me if you like -- the above
passage is my fault).

I've submitted a bug to the sourceforge tracker for Python, but
couldn't see how to assign it to anyone. Bug number is 792656, hope
this is what you meant.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top