Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Misleading description of [i:j:k] slicing?

Reply
Thread Tools

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

 
 
Raoul Gough
Guest
Posts: n/a
 
      08-21-2003
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?

--
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      08-21-2003
Raoul Gough <> writes:

> 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


--
This same programmer had worked for the military, and therefore had
access to weapons-grade cursing technology.
-- Matt Roberds, asr
 
Reply With Quote
 
 
 
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      08-21-2003
On Thu, 21 Aug 2003 17:12:28 GMT, rumours say that Michael Hudson
<> might have written:

>> "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?
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
 
Reply With Quote
 
Raoul Gough
Guest
Posts: n/a
 
      08-21-2003
Christos "TZOTZIOY" Georgiou <> writes:

> On Thu, 21 Aug 2003 17:12:28 GMT, rumours say that Michael Hudson
> <> might have written:
>
>>> "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).

--
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta
 
Reply With Quote
 
Raoul Gough
Guest
Posts: n/a
 
      08-21-2003
Michael Hudson <> writes:

> Raoul Gough <> writes:
>> 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).


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.

--
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Misleading error message when opening a file (on Windows XP SP 2) Claudio Grondi Python 8 08-28-2006 08:12 PM
misleading prefix ++ LuciferLeo@gmail.com Python 6 05-21-2006 01:03 PM
Nitpicking - slightly misleading traceback Juho Schultz Python 2 01-26-2006 02:48 PM
Misleading Python error message Brian Kelley Python 4 11-21-2003 08:30 AM
The term "global variables" misleading? j C Programming 5 07-27-2003 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57