Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Silly question

0 views
Skip to first unread message

David C Ullrich

unread,
Aug 20, 2009, 2:13:35 PM8/20/09
to
I just noticed that

sequence[i:j:k]

syntax in a post here. When did this happen?

(I'm just curious whether it existed in 1.5.x or not.
If so I'm stupid - otoh if it was introduced in 2.x
I'm just slow...)

Benjamin Kaplan

unread,
Aug 20, 2009, 2:36:35 PM8/20/09
to pytho...@python.org

Well, I got some good news and some bad news. According to the docs,
it existed in 1.4 but the built-in sequences didn't support it until
2.3. It's not used that often anyway so you haven't been missing much.

http://www.python.org/doc/2.3.5/whatsnew/section-slices.html

Duncan Booth

unread,
Aug 20, 2009, 2:41:34 PM8/20/09
to
David C Ullrich <dull...@sprynet.com> wrote:

Googling for 'python extended slice' returns this as the first hit:

http://www.python.org/doc/2.3.5/whatsnew/section-slices.html

> 15 Extended Slices
>
> Ever since Python 1.4, the slicing syntax has supported an optional
> third ``step'' or ``stride'' argument. For example, these are all
> legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to
> Python at the request of the developers of Numerical Python, which
> uses the third argument extensively. However, Python's built-in list,
> tuple, and string sequence types have never supported this feature,
> raising a TypeError if you tried it. Michael Hudson contributed a
> patch to fix this shortcoming.

So extended slices have existed since Python 1.4, but builtin types only
started to support them from 2.3.

David C Ullrich

unread,
Aug 20, 2009, 3:31:30 PM8/20/09
to

Fine (I knew they existed in Numerical Python way back when...)


David C Ullrich

unread,
Aug 20, 2009, 3:33:05 PM8/20/09
to
On Thu, 20 Aug 2009 14:36:35 -0400, Benjamin Kaplan wrote:

> On Thu, Aug 20, 2009 at 2:13 PM, David C Ullrich<dull...@sprynet.com>
> wrote:
>> I just noticed that
>>
>>  sequence[i:j:k]
>>
>> syntax in a post here. When did this happen?
>>
>> (I'm just curious whether it existed in 1.5.x or not. If so I'm stupid
>> - otoh if it was introduced in 2.x I'm just slow...)
>>
>>
> Well, I got some good news and some bad news. According to the docs, it
> existed in 1.4 but the built-in sequences didn't support it until 2.3.
> It's not used that often anyway so you haven't been missing much.

So I'm slow, fine. (There were several times when I was using 1.5.3
and wished they were there - transposing matrices, etc.)

>
> http://www.python.org/doc/2.3.5/whatsnew/section-slices.html

Aahz

unread,
Aug 20, 2009, 7:51:00 PM8/20/09
to
In article <mailman.143.1250793...@python.org>,
Benjamin Kaplan <benjami...@case.edu> wrote:
>On Thu, Aug 20, 2009 at 2:13 PM, David C Ullrich<dull...@sprynet.com> wrot=

>e:
>>
>> I just noticed that
>> sequence[i:j:k]
>
>Well, I got some good news and some bad news. According to the docs,
>it existed in 1.4 but the built-in sequences didn't support it until
>2.3. It's not used that often anyway so you haven't been missing much.

Except that it's canonical for one specific operation:

'reverseme'[::-1]
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"Given that C++ has pointers and typecasts, it's really hard to have a
serious conversation about type safety with a C++ programmer and keep a
straight face. It's kind of like having a guy who juggles chainsaws
wearing body armor arguing with a guy who juggles rubber chickens wearing
a T-shirt about who's in more danger." --Roy Smith

John Machin

unread,
Aug 20, 2009, 8:45:11 PM8/20/09
to
On Aug 21, 5:33 am, David C Ullrich <dullr...@sprynet.com> wrote:

> So I'm slow, fine. (There were several times when I was using 1.5.3
> and wished they were there - transposing matrices, etc.)

1.5.THREE ??

David C Ullrich

unread,
Aug 21, 2009, 2:36:02 PM8/21/09
to

Not sure. 1.SOMETHING. Sorry about the CONFUSION...

David C Ullrich

unread,
Aug 21, 2009, 3:40:30 PM8/21/09
to
On Thu, 20 Aug 2009 16:51:00 -0700, Aahz wrote:

> In article <mailman.143.1250793...@python.org>,
> Benjamin Kaplan <benjami...@case.edu> wrote:
>>On Thu, Aug 20, 2009 at 2:13 PM, David C Ullrich<dull...@sprynet.com>
>>wrot= e:
>>>
>>> I just noticed that
>>> sequence[i:j:k]
>>
>>Well, I got some good news and some bad news. According to the docs, it
>>existed in 1.4 but the built-in sequences didn't support it until 2.3.
>>It's not used that often anyway so you haven't been missing much.
>
> Except that it's canonical for one specific operation:
>
> 'reverseme'[::-1]

It's like you guys are a bunch of programmers or something:

from math import sqrt

def Primes(n):
"""Return a list of the primes < n"""
sieve = range(n)
for k in range(2,int(sqrt(n))+2):
sieve[::k] = [1]*((n+k-1)/k)
return [p for p in sieve if p > 1]


David C Ullrich

unread,
Aug 21, 2009, 3:45:55 PM8/21/09
to

Oops. Should have tested that a little more carefully
before posting. No time to fix it right now, customer just
got here. Let's just say we're looking for the primes
between sqrt(n) and n...

David C. Ullrich

unread,
Aug 21, 2009, 7:32:41 PM8/21/09
to
On Fri, 21 Aug 2009 14:45:55 -0500, David C Ullrich
<dull...@sprynet.com> wrote:

>[...]


>
>Oops. Should have tested that a little more carefully
>before posting. No time to fix it right now, customer just
>got here. Let's just say we're looking for the primes
>between sqrt(n) and n...

from math import sqrt

def Primes(n):
"""Return a list of the primes < n"""
sieve = range(n)
for k in range(2,int(sqrt(n))+2):

sieve[2*k::k] = [1]*((n-k-1)/k)


return [p for p in sieve if p > 1]


David C. Ullrich

"Understanding Godel isn't about following his formal proof.
That would make a mockery of everything Godel was up to."
(John Jones, "My talk about Godel to the post-grads."
in sci.logic.)

0 new messages