Slicing with negative strides

S

Steven D'Aprano

Does anyone here use slices (or range/xrange) with negative strides other
than -1?

E.g. sequence[2:15:-3]


If so, there is a discussion (long, long, looooooong discussion) on the
python-ideas mailing list, debating whether or not to deprecate or change
the behaviour of slicing with negative strides. So if you care about the
current behaviour, now is the time to stand up and be counted.

(Standing up *here* is fine, don't feel that you have to join yet another
list.)
 
M

Mark Lawrence

Steven D'Aprano said:
Does anyone here use slices (or range/xrange) with negative strides
other than -1?

E.g. sequence[2:15:-3]

With any negative stride your example is just the empty sequence.
If so, there is a discussion (long, long, looooooong discussion) on
the python-ideas mailing list, debating whether or not to deprecate or
change the behaviour of slicing with negative strides. So if you care
about the current behaviour, now is the time to stand up and be
counted.

(Standing up *here* is fine, don't feel that you have to join yet
another list.)
For those of us that don't really want to join another mailing list, could
you summarise what change is being proposed?

Umpteen options have been put forward. IMHO the bookies favourite is
currently being endorsed by Tim Peters and Terry Reedy, yours odds may
vary :)
 
T

Terry Reedy

Steven D'Aprano said:
Does anyone here use slices (or range/xrange) with negative strides
other than -1?

E.g. sequence[2:15:-3]

With any negative stride your example is just the empty sequence.

The idea is that one would not have to reverse 2 and 15 to get a
non-empty sequence.
 
M

Martin Manns

Does anyone here use slices (or range/xrange) with negative strides
other than -1?

I have used negative strides for comparing discrete sequences e. g. for
turbulence analysis, and I hope that my code will still run in Python 4.

Martin
 
S

Steven D'Aprano

I have used negative strides for comparing discrete sequences e. g. for
turbulence analysis, and I hope that my code will still run in Python 4.

Can you show us a typical example of how you would normally use such
negative strides?


Thanks,
 
P

Peter Cacioppi

Does anyone here use slices (or range/xrange) with negative strides other

than -1?

Non default positive strides are very handy, but negative strides seem weird to me. Not the negative striding exactly, but the way fenceposts and negative strides interact.

For example, this poster seems to posit a canonical WTF with negative strides.

http://stackoverflow.com/questions/5798136/python-reverse-stride-slicing

I can almost picture Picard saying "WTF do you need to omit the end index to get the zero element??!!"

Readability counts, no? Just reverse it and use positive strides.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top