473,767 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

negative stride list slices

Can anyone explain the logic behind the behavior of list slicing with
negative strides? For example:
print range(10)[:-3:-1]

[9,8]

I found this result very surprising, and would just like to see the
rules written down somewhere.

Thanks,
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
Jul 18 '05
19 2608
Shalabh Chaturvedi wrote:
I'd like to add:

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
| |
start end (defaults for +ve step)
end start (defaults for -ve step)

(Is this correct?)


Apparently not.
"HelpA"[-1:-2:-1] 'A'

I would expect 'p'. Also,
"HelpA"[:-1:-1]

''

And I definitely expected 'A'. Now I find it confusing too.

Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
reverse of S[a:b:1]?

This is only true for S[::1] and S[::-1]. In other cases, for -ve step,
the -ve indices seem to indicate a shifted position. I'm using Python 2.3.3.

--
Shalabh
Jul 18 '05 #11
(Please don't top-post. It really buggers up the quoting
and makes discussion hard.)

Julio Oña wrote:
Slice has three arguments, [begin:end:step]

when doing s[:-3:-1] you are asking the las to elements of the list in
reversed order.


Uh, yeah. Okay. So let's say that part was obvious. Now
please explain *which* elements are being listed in reverse
order, referring to the index value -3 and the elided
index value. Presumably one of those refers to the beginning
or end of the list, while the other one refers to something
that is referenced as "-3". Can you point to a diagram or
description in the documentation or tutorial which actually
explains this? Or can you (or anyone) explain it in your
own words? Or is everyone who thinks this makes sense just
pretending to actually understand it?

-Peter
Jul 18 '05 #12
On Wed, 01 Sep 2004 18:05:22 -0400, Peter Hansen <pe***@engcorp. com> wrote:

Uh, yeah. Okay. So let's say that part was obvious. Now
please explain *which* elements are being listed in reverse
order, referring to the index value -3 and the elided
index value. Presumably one of those refers to the beginning
or end of the list, while the other one refers to something
that is referenced as "-3". Can you point to a diagram or
description in the documentation or tutorial which actually
explains this? Or can you (or anyone) explain it in your
own words? Or is everyone who thinks this makes sense just
pretending to actually understand it?


Sorry Peter to be to lassy.

Well in the case [:-3:-1] I think this is what is happening
- revers the list
- this left with [0:2] (remember [-3:] returns 2 characters only

in the case [-1:-2:-1]
- revers the string 'Apleh'
- this left with [:1] (this is tricky but -1 is equivalent to 0 as
-2 is to 1, remember you are counting backwards from the end)

The problem is the mening of -1 when reversing the list. The slice is
closed in the lower side and open in the upper one. When reversing the
list the lower end is open and no close as with positive step.

Any comments?
--
Julio
Jul 18 '05 #13
Peter Hansen wrote:
(Please don't top-post. It really buggers up the quoting
and makes discussion hard.)

Julio Oña wrote:
Slice has three arguments, [begin:end:step]

when doing s[:-3:-1] you are asking the las to elements of the list in
reversed order.

Uh, yeah. Okay. So let's say that part was obvious. Now
please explain *which* elements are being listed in reverse
order, referring to the index value -3 and the elided
index value. Presumably one of those refers to the beginning
or end of the list, while the other one refers to something
that is referenced as "-3". Can you point to a diagram or
description in the documentation or tutorial which actually
explains this? Or can you (or anyone) explain it in your
own words? Or is everyone who thinks this makes sense just
pretending to actually understand it?

I've been using extended slicing in Numeric and now numarray for years
and I even wrote some classes that implement extended slicing, but I
still often find it confusing to use negative indices in practice.
However, despite that confusion, I'll give a shot at explaining it.

There are (at least) two issues: what to do with missing indices and
given all the indices, what does it all mean. I'll start with the first
issue, since it's simpler. Of the three indices (start, stop and step),
only start and stop are potentially confusing, since step simply
defaults to 1. Start defaults to the first element (0) if step is
greater than zero, otherwise it defaults to the last element (len(s)-1).
Stop, on the other hand, defaults to one past the edge of the sequence.
If step is positive, it's one past the far edge (len(s)), otherwise it's
one past the near edge. One would be tempted to write this as -1, but
that won't works since that means the end of the sequence, so we'll call
it (-len(s)-1). In summary the missing indices are supplied as:

start = 0 if (step > 0) else len(s)-1
stop = len(s) is (step > 0) else -len(s)-1
step = 1

On to issue number two. I've always understood extended slicing in
relation to the range. In Numeric this relation is best expressed using
Numeric.take, however for general python consumption I'll cast it in
terms of list comprehensions. Since we've already disposed of how to
supply the missing parts of the slice, this will only deal with the case
where all parts are supplied. In addition, if start or stop is less than
zero, we add len(s) so that all values are nonnegative unless stop was
-len(s)-1, in which case it ends up as -1. With that wordy preamble,
here's how I've always understood extended slicing:

s[start:stop:step] ~ [s[i] for i in range(start,sto p,step)]

Let me show a few examples:

s = 'python' s[:-3:-1] 'no' s[len(s)-1:-3:-1] # Fill in the default values 'no' [s[i] for i in range(len(s)-1, len(s)-3,-1)] # equiv to s[:-3:-1] ['n', 'o']
s[4::] 'on' s[4:len(s):1] # Fill in the default values 'on' [s[i] for i in range(4,len(s), 1)] # equiv to s[4::] ['o', 'n']
s[4::-1] 'ohtyp' s[4:-len(s)-1:-1] # Fill in the default values 'ohtyp' [s[i] for i in range(4,-1,-1)] # equiv to s[4::-1]

['o', 'h', 't', 'y', 'p']
Is this confusing? Probably. However, I suspect it's a natural
consequence of Python's zero based indexing. Zero based indexing is
great in a lot of ways, but it has some unfortunate corner cases for
negative indices (another topic) and gets downright odd for negative
strides. So it goes.

I-hope-I-didn't screw-that-up-too-badly-ly yours,

-tim

Jul 18 '05 #14
Julio Oña wrote:
On Wed, 01 Sep 2004 18:05:22 -0400, Peter Hansen <pe***@engcorp. com> wrote:
please explain *which* elements are being listed in reverse
order, referring to the index value -3 and the elided
index value.


Well in the case [:-3:-1] I think this is what is happening
- revers the list
- this left with [0:2] (remember [-3:] returns 2 characters only

in the case [-1:-2:-1]
- revers the string 'Apleh'
- this left with [:1] (this is tricky but -1 is equivalent to 0 as
-2 is to 1, remember you are counting backwards from the end)

The problem is the mening of -1 when reversing the list. The slice is
closed in the lower side and open in the upper one. When reversing the
list the lower end is open and no close as with positive step.

Any comments?


Okay, so let me see how this looks with the previous diagram.

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1

But that only works for the forward or positive step case.
With a negative/reverse step, you first have to reverse
the string:

+---+---+---+---+---+
| A | p | l | e | H |
+---+---+---+---+---+
-1 -2 -3 -4 -5 -6

And now you're in effect going from the start (i.e. the
missing value before the colon) to the -3 point.

Thus 'Ap'.

I don't find it at all intuitive, and certainly the existing
diagram needs to be updated if this is really how it works,
but at least you have an explanation that corresponds to the
values, so thank you.

It does seem very odd that the step of -1 in effect changes
the normal meaning or position of the slice indices, by
first reversing the string. I'm guessing that this is
necessary because any other interpretation leads to even
worse situations than the confusion this inspires, but
perhaps that's not true.

I would have found either 'Apl' or 'eH' more intuitive...

-Peter
Jul 18 '05 #15
On Wed, 01 Sep 2004 14:57:57 -0700, Shalabh Chaturvedi wrote:
Shalabh Chaturvedi wrote:
I'd like to add:

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
| |
start end (defaults for +ve step)
end start (defaults for -ve step)

(Is this correct?)
Apparently not.


That should be

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
| |
start end (defaults for +ve step)
end start (defaults for -ve step)
>>> "HelpA"[-1:-2:-1] 'A'

I would expect 'p'. Also,
>>> "HelpA"[:-1:-1]
''

And I definitely expected 'A'. Now I find it confusing too.

Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
reverse of S[a:b:1]?


As others have explained, that's not easily possible because of Python's
zero-based indexing, which in general is great, but can lead to confusion
because using negative strides indices begin with -1.

This is only true for S[::1] and S[::-1]. In other cases, for -ve step,
the -ve indices seem to indicate a shifted position. I'm using Python 2.3.3.


The defaults here map to S[0:len(S):1] and S[-1:-len(S)-1:-1].

--
Christopher

Jul 18 '05 #16
On Wed, 1 Sep 2004, Shalabh Chaturvedi wrote:
Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
reverse of S[a:b:1]?


No. S[a:b:1] includes the start index (a), but excludes the stop index
(b). Similarly, S[b:a:-1] includes the start index (b), but excludes the
stop index (a). Only if the stop indices were included (or the start
indices were excluded), thus making the indexing symmetrical, would I
expect the results to be symmetric.

Jul 18 '05 #17
da**@boost-consulting.com (David Abrahams) writes:
Can anyone explain the logic behind the behavior of list slicing with
negative strides?
You can read PySlice_GetIndi cesEx in Objects/sliceobject.c if you're
really keen...
For example:
>>> print range(10)[:-3:-1]
[9,8]

I found this result very surprising,


What would you have expected instead?

range(10-1, 10-3, -1) == [9,8]

was my guiding principle when I wrote the code.
and would just like to see the rules written down somewhere.


It seems to be hardly any easier to write the rules than to write the
algorithm, in practice...

In practice, I only ever use negative strides as "[::-1]", the result
of which hopefully surprises noone.

Cheers,
mwh

--
<glyph> yo ho ho and a bottle of internet
-- from Twisted.Quotes
Jul 18 '05 #18
Christopher T King wrote:
On Wed, 1 Sep 2004, Shalabh Chaturvedi wrote:

Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
reverse of S[a:b:1]?

No. S[a:b:1] includes the start index (a), but excludes the stop index
(b). Similarly, S[b:a:-1] includes the start index (b), but excludes the
stop index (a). Only if the stop indices were included (or the start
indices were excluded), thus making the indexing symmetrical, would I
expect the results to be symmetric.


That's a good explanation. My initial confusion arose because I
remembered how slices work by the suggestion (and diagram) under:

http://docs.python.org/tut/node5.htm...00000000000000

"The best way to remember how slices work is to think of the indices as
pointing /between/ characters". When negative step is in the picture,
the indices are no longer in the same inbetween places and need to be
shifted to the right. Associating indices with characters themselves
dissipates the confusion.

Thanks,
Shalabh

Jul 18 '05 #19
Shalabh Chaturvedi <sh*****@cafepy .com> wrote:
...
Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
reverse of S[a:b:1]?


Not me, because I _know_ that S[x:y:anything] INCLUDES S[x] and EXCLUDES
S[y]. So, swappiing x and y can't POSSIBILY "exactly reverse' things,
*ever*, NO MATTER HOW 'anything' changes.

That's a good reason why 2.4 introduces the new built-in 'reversed': if
you want to loop on "exactly the reverse of" S[a:b], you loop on
reversed(S[a:b]) and live happily ever after.
Alex
Jul 18 '05 #20

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

Similar topics

21
4332
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i in l] but I was hoping for something more like l. Hilde
3
7532
by: Simon | last post by:
Hi, I'm hoping you could show me examples of how a functional/declarative language could be used to consicely describe resticted subsets of elements. I'm looking for a 'specification' style definition so any ideas/input would be very welcome. Thanks for your time, Simon. --
29
3408
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I wonder why it is not implemented for tuples. George
65
4236
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the difference is between these and: (3) mylist =
77
17056
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it, sets have it, why do lists have to be second class citizens?
5
1811
by: micklee74 | last post by:
hi i have a list with contents like this alist = how can i "convert" this list into a dictionary such that dictionary = { '>QWER':'askfhs' , '>REWR' : 'sfsdf' , '>FGDG', 'sdfsdgffdgfdg' }
19
2909
by: George Sakkis | last post by:
It would be useful if list.sort() accepted two more optional parameters, start and stop, so that you can sort a slice in place. In other words, x = range(1000000) x.sort(start=3, stop=-1) would be equivalent to x = sorted(x)
2
3892
by: ajcppmod | last post by:
I'm really confused about results of slices with negative strides. For example I would have then thought of the contents of mystr as: indices 0 1 2 3 4 5 6 7 8 content m y s t r i n g with mystr = 'my '
5
3838
by: NuberSteve | last post by:
I'm very new to using CSS and also the concept of slices for mouse-overs, and have made my first attempt at using ImageReady to generate slices of a world map. I basically wanted a map that would show various countries appearing to be depressed when "moused-over". To keep it simple at first, I just decided to try two countries. After copying the HTML and JavaScript codes generated by ImageReady into the page I wanted to insert the map into,...
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8838
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7383
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3930
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.