473,396 Members | 2,029 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,396 software developers and data experts.

a question about s[i:j] when i is negative

s[i:j] 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.
(4)
The slice of s from i to j is defined as the sequence of items with
index k such that i <= k < j. If i or j is greater than len(s), use
len(s). If i is omitted or None, use 0. If j is omitted or None, use
len(s). If i is greater than or equal to j, the slice is empty.

i can't follow (3) very well.
for example:k = [1,2,3,4,5]
k[-1:2]=[]
k[-5:2]=[1,2]
but k[-6:2] = [1,2]
why k[-6:2] is [1,2]not [].i do follow (3),to make i positive by
plusing len(k) twice.But the result is not what i expect.why,or did i
misunderstand the rule of (3).

Oct 17 '06 #1
5 1011
dracula571 wrote:
but k[-6:2] = [1,2]
why k[-6:2] is [1,2]not [].i do follow (3),to make i positive by
plusing len(k) twice.
twice?

</F>

Oct 17 '06 #2

dracula571 wrote:
s[i:j] 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.
(4)
The slice of s from i to j is defined as the sequence of items with
index k such that i <= k < j. If i or j is greater than len(s), use
len(s). If i is omitted or None, use 0. If j is omitted or None, use
len(s). If i is greater than or equal to j, the slice is empty.

i can't follow (3) very well.
for example:k = [1,2,3,4,5]
k[-1:2]=[]
k[-5:2]=[1,2]
but k[-6:2] = [1,2]
why k[-6:2] is [1,2]not [].
Because 6 is greater then len(k), so when you ask
for i=-6, you get i=-5 instead. And relative -5 is actually
absolute 0 which is less than 2, so you do not get an
empty slice.
i do follow (3),to make i positive by
plusing len(k) twice.But the result is not what i expect.why,or did i
misunderstand the rule of (3).
See (4).

Oct 17 '06 #3

Fredrik Lundh 写道:
dracula571 wrote:
but k[-6:2] = [1,2]
why k[-6:2] is [1,2]not [].i do follow (3),to make i positive by
plusing len(k) twice.
twice?

</F>
that is -6 + len(k) + len(k) =4

Oct 17 '06 #4

me********@aol.com 写道:
dracula571 wrote:
s[i:j] 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.
(4)
The slice of s from i to j is defined as the sequence of items with
index k such that i <= k < j. If i or j is greater than len(s), use
len(s). If i is omitted or None, use 0. If j is omitted or None, use
len(s). If i is greater than or equal to j, the slice is empty.

i can't follow (3) very well.
for example:k = [1,2,3,4,5]
k[-1:2]=[]
k[-5:2]=[1,2]
but k[-6:2] = [1,2]
why k[-6:2] is [1,2]not [].

Because 6 is greater then len(k), so when you ask
for i=-6, you get i=-5 instead. And relative -5 is actually
absolute 0 which is less than 2, so you do not get an
empty slice.
i do follow (3),to make i positive by
plusing len(k) twice.But the result is not what i expect.why,or did i
misunderstand the rule of (3).

See (4).
when compared with len(k), the value of i is a absolute value?
in this example,i used -6 to compare with len(k).
I misunstood the rule (4),and used (3) wrong by plusing len(k) twice to
make it positive ,that is -6 + len(k) + len(k) = 4.
Thanks

Oct 17 '06 #5
"dracula571" wrote:
>dracula571 wrote:
but k[-6:2] = [1,2]
why k[-6:2] is [1,2]not [].i do follow (3),to make i positive by
plusing len(k) twice.

twice?

that is -6 + len(k) + len(k) =4
sure, but what makes you think you should add the length twice?

</F>

Oct 17 '06 #6

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

Similar topics

6
by: cylin | last post by:
Hi all, If I have a variable and it's just have 22 bits, and I must set this variable unsigned integer. This 2's complement value maybe is negative. For example: A= 11 1111 1111 1110 ...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
6
by: Devrim GUNDUZ | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, We were performing some tests on PostgreSQL and found that it fails on the following query: SELECT 512*18014398509481984 AS result;
7
by: Jeremy | last post by:
hello, I am trying to calculate the standard deviation with my program, but I ran into a small problem with squaring negative value's. Which cannot be done.. Ehm let me explain let's say; ...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
10
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
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...
20
by: xianwei | last post by:
int MaxSubsequenceSum( const int A, int N ) { int ThisSum, MaxSum, j; ThisSum = MaxSum = 0; for (j = 0; j < N; j++) { ThisSum += A;
0
by: hi | last post by:
The truth about Prophet Muhammad (Peace Be Upon Him) Loving him is following him Muslims all over the world are deeply hurt by the recent caricatures of our beloved Prophet Muhammad , in...
5
by: imish06 | last post by:
This is the question: "The user must enter a non-negative number to add to the lists or a negative number to stop the program. For every non-negative number entered, place each even and odd...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
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
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...

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.