473,414 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

a simple question about the third index in slice

I cannot quite understand when the third index is a negative
number,like this:
a = '0123456789'
a[1:10:2] I know the index step is 2, so it will collect items from
offset 1, 3, 5, 7, 9
but when a negative number come,like:
a[1::-1] answer '10', and a[1:10:-1] only answer '',
what is the different between the two expression, where does the offset

begin and what is the end offset?
Thanks!

Oct 21 '05 #1
1 1517
someone wrote:
I cannot quite understand when the third index is a negative
number,like this:
a = '0123456789'
a[1:10:2] I know the index step is 2, so it will collect items from
offset 1, 3, 5, 7, 9
but when a negative number come,like:
a[1::-1] answer '10', and a[1:10:-1] only answer '',
what is the different between the two expression, where does the offset
begin and what is the end offset?


the default values for start and stop depend on the sign of the step.

you can use the slice type directly to see how this works:
slice(None,None,1).indices(10) (0, 10, 1) slice(None,None,-1).indices(10) (9, -1, -1)
slice(1,None,-1).indices(10) (1, -1, -1) slice(1,10,-1).indices(10)

(1, 10, -1)

also see footnote 5 on this page:

http://docs.python.org/lib/typesseq.html

</F>

Oct 21 '05 #2

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

Similar topics

2
by: Uwe Mayer | last post by:
Hi, a class of mine should support the list interface and implements the __len__ and __getitem__ methods. Now when I ask for an unbounded slice: >>> len( myObj ) my __getitem__(self, y)...
9
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be made more efficient. Right now if you do use a...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
65
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...
11
by: John Salerno | last post by:
Given: numbers = can someone explain to me why numbers results in ? I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this...
5
by: dracula571 | last post by:
s slice of s from i to j (3), (4) (3) If i or j is negative, the index is relative to the end of the string: len(s) + i or len(s) + j is substituted. But note that -0 is still 0. ...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
8
by: John Salerno | last post by:
Hey all. I've decided I let my Python skills (minor though they were) slip away so I started reading the new edition of Learning Python to brush up. I just read about lists again and I'm wondering...
3
by: jmDesktop | last post by:
This program: s = 'abcde' i = -1 for i in range (-1, -len(s), -1): print s, i gives abcd -1
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.