473,750 Members | 2,571 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 #1
19 2605
David Abrahams wrote:
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


http://www.python.org/doc/current/tu...00000000000000
Jul 18 '05 #2
"Reid Nichol" <rn*********@ya hoo.com> wrote in message
news:r7******** **********@news 1.mts.net...
David Abrahams wrote:
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.

http://www.python.org/doc/current/tu...00000000000000

You probably meant to reference the next section (that bookmark relates to
strings, the next one is lists), but in either case none of the examples in
the tutorial has yet been updated to deal with slices that include a
"stride" as a third parameter.
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #3
David Abrahams wrote:
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.


Well, here is my attempt to emulate the algorithm. The trick seems to be to
substitute the start/stop parameters with 0 or length-1 depending on the
sign of step.

# no warranties!
def indices(length, start, stop, step):
if step is None:
step = 1
if step < 0:
if start is None:
start = length-1
elif start < 0:
start += length

if stop is None:
stop = -1
elif stop < 0:
stop += length
else:
if start is None:
start = 0
elif start < 0:
start += length

if stop is None:
stop = length
elif stop < 0:
stop += length

if start > stop:
while start > stop:
yield start
start += step
else:
while start < stop:
yield start
start += step

assert list(indices(10 , None, -3, -1)) == range(10)[:-3:-1]
assert list(indices(10 , None, -3, -2)) == range(10)[:-3:-2]
assert list(indices(10 , 9, -3, -2)) == range(10)[9:-3:-2]
assert list(indices(10 , None, None, None)) == range(10)[::]
assert list(indices(10 , None, 5, 2)) == range(10)[:5:2]

I have to admit (late but better than never) that Raymond Hettinger's new
builtin reversed() has some merits:
list(reversed(r ange(10)[-2:])) == range(10)[:-3:-1]

True

Peter

Jul 18 '05 #4
Russell Blau wrote:
"Reid Nichol" <rn*********@ya hoo.com> wrote in message
news:r7******** **********@news 1.mts.net...
David Abrahams wrote:
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.

http://www.python.org/doc/current/tu...00000000000000

You probably meant to reference the next section (that bookmark relates to
strings, the next one is lists), but in either case none of the examples in
the tutorial has yet been updated to deal with slices that include a
"stride" as a third parameter.

This was the part that I was refering to:

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
Does it not all work the same in practice?
Jul 18 '05 #5
Reid Nichol wrote:
This was the part that I was refering to:

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
Does it not all work the same in practice?

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?)

David Abrahams wrote: 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


What were you expecting?

Thanks,
Shalabh

Jul 18 '05 #6
Reid Nichol wrote:
This was the part that I was refering to:

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

Does it not all work the same in practice?


Can you explain how the above diagram explains David's
example then:
s = 'HelpA'
s[:-3:-1]

'Ap'

I don't find much connection between the diagram and
where the -3 is actually slicing either.

-Peter
Jul 18 '05 #7
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.

regards.

On Wed, 01 Sep 2004 17:15:51 -0400, Peter Hansen <pe***@engcorp. com> wrote:
Reid Nichol wrote:
This was the part that I was refering to:

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

Does it not all work the same in practice?


Can you explain how the above diagram explains David's
example then:
s = 'HelpA'
s[:-3:-1]

'Ap'

I don't find much connection between the diagram and
where the -3 is actually slicing either.

-Peter
--
http://mail.python.org/mailman/listinfo/python-list

--
Julio
Jul 18 '05 #8
http://www.python.org/doc/2.3.4/what...on-slices.html

There is more information on extended slicing

Regards.

On Wed, 1 Sep 2004 17:23:18 -0400, Julio Oña <th******@gmail .com> 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.

regards.



On Wed, 01 Sep 2004 17:15:51 -0400, Peter Hansen <pe***@engcorp. com> wrote:
Reid Nichol wrote:
This was the part that I was refering to:

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

Does it not all work the same in practice?


Can you explain how the above diagram explains David's
example then:
>> s = 'HelpA'
>> s[:-3:-1]

'Ap'

I don't find much connection between the diagram and
where the -3 is actually slicing either.

-Peter
--
http://mail.python.org/mailman/listinfo/python-list



--
Julio

--
Julio
Jul 18 '05 #9
"Shalabh Chaturvedi" <sh*****@cafepy .com> wrote in message
news:ma******** *************** *************** @python.org...
Reid Nichol wrote:
This was the part that I was refering to:

+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
Does it not all work the same in practice?

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?)


The tutorial says, "The slice from i to j consists of all characters between
the edges labeled i and j, respectively." So [1:3] starts at the left edge
of the character with index 1, and ends at the left edge of the character
with index 3, returning "el". However, a slice from 3:1:-1 starts at the
*right* edge of the character with index 3, and ends at the *right* edge of
the character with index 1, returning "pl". So the above diagram is not
correct for negative steps (the "backwards" slice consists of characters
between the edges labeled j+1 and i+1, in reverse order).

Is this difference intended, or a bug? (One consequence of this difference
is that a slice of [5:0:-1] returns something different from a slice of [5:
:-1]!)

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #10

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

Similar topics

21
4328
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
3405
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
4229
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
17049
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
1809
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
2908
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
3890
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
3837
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
9001
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
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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...
1
6808
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
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
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
2807
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.