A suggestion for a possible Python module

Alex Martelli aleax at aleax.it
Tue Mar 4 11:45:08 EST 2003


Terry Reedy wrote:

> 
> "Alex Martelli" <aleax at aleax.it> wrote in message
> news:nKZ8a.2029$zo2.77091 at news2.tin.it...
>> Still, I do think Gries' algorithm would be a good reason to add
>> optional start and stop arguments to Python lists' *reverse*
>> method.  Hmmm...
> 
> Pending such an addition (which I would like) would something using
> the new 2.3 stride feature work? Something like (untested, since I
> don't have 2.3 yet):
> 
> alist[start:stop] = alist[stop:start:-1] # But at least twice as slow
> as in-place reverse

Yes, and the boundaries play strangely...:

>>> x = list('arrivederci')
>>> x[2:7] = x[7:2:-1]
>>> ''.join(x)
'aredevierci'
>>>

x[2:7] being 'rived', one would expect 'ardevirerci' -- but to
get that one needs an ample application of -1's, alas...:

>>> x = list('arrivederci')
>>> x[2:7] = x[7-1:2-1:-1]
>>> ''.join(x)
'ardevirerci'
>>>

Goofy, slow, and error-prone, I think there's little to be said
for this idiom (x[start:stop] = x[stop-1:start-1:-1] in the
general case).


Alex





More information about the Python-list mailing list