By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

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

P: n/a

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

P: n/a

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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.