473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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
Jul 18 '05 #1
4 1558
Raoul Gough <Ra********@yahoo.co.uk> 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
Jul 18 '05 #2
On Thu, 21 Aug 2003 17:12:28 GMT, rumours say that Michael Hudson
<mw*@python.net> 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.
Jul 18 '05 #3
Christos "TZOTZIOY" Georgiou <tz**@sil-tec.gr> writes:
On Thu, 21 Aug 2003 17:12:28 GMT, rumours say that Michael Hudson
<mw*@python.net> 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
Jul 18 '05 #4
Michael Hudson <mw*@python.net> writes:
Raoul Gough <Ra********@yahoo.co.uk> 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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files...
9
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma....
3
by: Bryan Olson | last post by:
I recently wrote a module supporting value-shared slicing. I don't know if this functionality already existed somewhere, but I think it's useful enough that other Pythoners might want it, so here...
11
by: jbperez808 | last post by:
>>> rs='AUGCUAGACGUGGAGUAG' >>> rs='GAG' Traceback (most recent call last): File "<pyshell#119>", line 1, in ? rs='GAG' TypeError: object doesn't support slice assignment You can't assign to...
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
0
by: Fehy | last post by:
The code below, while compiled with .NET 2003, produce misleading error description: c:\Work\error\exmaple\exmaple\exmaple.cpp(17) : error C2501: 'makeerror' : missing storage-class or type...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
2
by: Rahul | last post by:
Hi Everyone, I was working around object slicing (pass by value) and was wondering how it is specified in the standard, class A { }; class B: public A
1
by: subramanian100in | last post by:
Consider class Base { .... }; class Derived : public Base { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.