473,651 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sequence slicing documentation

In trying to work out what's different between the start, stop and step
of slice.indices() and the start, stop and step of sequence slicing[1] I
found that some of the list slicing documentation[2] is vague. I'd like
to submit a documentation fix, but I want to make sure I have it right.
Here's what points (3) and (5) of the Sequence Types documentation say
now:

"""
(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.
....
(5) The slice of s from i to j with step k is defined as the sequence of
items with index x = i + n*k such that $0 \leq n < \frac{j-i}{k}$. In
other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping
when j is reached (but never including j). If i or j is greater than
len(s), use len(s). If i or j are omitted then they become ``end''
values (which end depends on the sign of k). Note, k cannot be zero.
"""

I'd like to replace that second-to-last sentence in point (5) with the
vague ``end'' description with something more explicit. I'd like it to
read something like:

"""
If k is positive and i or j is omitted, they will be replaced with 0 and
len(s) respectively. If k is negative and i or j is omitted, they will
be replaced with -1 and -len(s)-1 respectively. Note that these
replacements happen before the rule from point (3) is applied.
"""

I'd also like to put an example with point (5) to show this behavior in
action. Something like:

"""
So for example::
seq = 'abcde'
len(seq) 5 'edc' == seq[:1:-1] == seq[-1:1:-1] True 'ca' == seq[2::-2] == seq[2:-5-1:-2] True

Note however that manually applying the rule from point (3) (adding
len(s) to any i or j that is negative) works for i, but does not work
for j::
seq[5-1:1:-1] 'edc' seq[2:-1:-2]

''

This is because Python sees the -1 in the j position and applies the
rule from point (3) again.
"""

If a few people could check over my logic here and make sure I'm not
completely misguided, I'll post a documentation fix.

Thanks,

STeVe

[1] http://mail.python.org/pipermail/pyt...st/293963.html
[2] http://docs.python.org/lib/typesseq.html
Aug 19 '05 #1
0 1054

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

Similar topics

11
1334
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 list, so what is it
9
1414
by: tkpmep | last post by:
I have a list y >>>y from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I tried >>>a,b = y Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: list indices must be integers
0
1542
by: rval | last post by:
I have this schema while teh following sequence <complexType name="SetOfTasks" mixed="true"> <sequence> <choice minOccurs="1" maxOccurs="unbounded"> <element minOccurs="0" ref="tns:task1" /> <element minOccurs="0" ref="tns:task2" /> <element minOccurs="0" ref="tns:task3" /> <element minOccurs="0" ref="tns:task4" /> <element minOccurs="0" ref="tns:task5" />
7
2438
by: schickb | last post by:
I think it would be useful if iterators on sequences had the __index__ method so that they could be used to slice sequences. I was writing a class and wanted to return a list iterator to callers. I then wanted to let callers slice from an iterator's position, but that isn't supported without creating a custom iterator class. Are there reasons for not supporting this generally? I realize not all iterators would have the __index__ method,...
0
8275
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
8792
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
8694
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
8457
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
8571
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
7294
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...
0
4143
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
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.