Re: [Tutor] Philosophical question about string slicing from end of a string

2014-11-24 Thread Zachary Ware
On Mon, Nov 24, 2014 at 12:32 PM, boB Stepp robertvst...@gmail.com wrote:
 Python 2.7.8
 Win7Pro

 str = 0123456789
 str[-1]
 '9'
 str[-3:-1]
 '78'
 str[-3:]
 '789'

 I understand that the above is the way it is in Python, but I am
 puzzled why the designers did not choose that str[-3:-1] returns
 '789', especially since str[-1] returns '9'. What is the reason for
 choosing Python's actual behavior?

For the same reason as this behavior with positive indices:

 s = '0123456789'
 s[1]
'1'
 s[3]
'3'
 s[1:3]
'12'

Here's what you can do with it:

 s[-5:-3] + s[-3:-1] == s[-5:-1]
True
 len(s[-5:-3]) == -3 - -5
True

Think of slicing as everything from this index (-3) up to but not
including (:) this index (-1).

Have I clarified or muddied it for you? :)

Regards,
-- 
Zach
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Philosophical question about string slicing from end of a string

2014-11-24 Thread boB Stepp
On Mon, Nov 24, 2014 at 12:57 PM, Zachary Ware
zachary.ware+py...@gmail.com wrote:
[...]

 Have I clarified or muddied it for you? :)

Clarified, I believe, if my following statements are correct: I did
not consider that the behavior was symmetric with positive indices.
So, index 0 is the center relative to which positive and negative
indices create identical behaviors.

Thanks!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Philosophical question about string slicing from end of a string

2014-11-24 Thread boB Stepp
On Mon, Nov 24, 2014 at 1:06 PM, boB Stepp robertvst...@gmail.com wrote:
 On Mon, Nov 24, 2014 at 12:57 PM, Zachary Ware
 zachary.ware+py...@gmail.com wrote:
 [...]

 Have I clarified or muddied it for you? :)

 Clarified, I believe, if my following statements are correct: I did
 not consider that the behavior was symmetric with positive indices.
 So, index 0 is the center relative to which positive and negative
 indices create identical behaviors.

Hmm. There is a flaw in my observed symmetry in that str[0:3] and
str[-3:0] do not behave the same as the latter returns an empty
string.

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Philosophical question about string slicing from end of a string

2014-11-24 Thread Zachary Ware
On Mon, Nov 24, 2014 at 1:06 PM, boB Stepp robertvst...@gmail.com wrote:
 On Mon, Nov 24, 2014 at 12:57 PM, Zachary Ware
 zachary.ware+py...@gmail.com wrote:
 [...]

 Have I clarified or muddied it for you? :)

 Clarified, I believe, if my following statements are correct: I did
 not consider that the behavior was symmetric with positive indices.
 So, index 0 is the center relative to which positive and negative
 indices create identical behaviors.

Hmm, either I'm not quite following you, or that's not quite right.
For the purposes of slicing, 0 is positive and slices that have either
all positive or all negative indices behave the same way.  Mixing
positive and negative indices is of course possible, but takes a
little bit more thought since you're counting from opposite ends.
Also note that there's no way to get the last member with a negative
second index.

[Received while writing the above]
On Mon, Nov 24, 2014 at 1:16 PM, boB Stepp robertvst...@gmail.com wrote:
 Hmm. There is a flaw in my observed symmetry in that str[0:3] and
 str[-3:0] do not behave the same as the latter returns an empty
 string.

Correct.  Here's another way to think about negative indices,
particularly for converting between positive and negative indices:

 s[-5:-3]
'56'
 len(s)-5
5
 len(s)-3
7
 s[len(s)-5:len(s)-3]
'56'
 s[5:7]
'56'

So, when you try to do s[-3:0], you're doing s[len(s)-3:0], which
equates to s[7:0].  Building from my example with the length of the
slice in my previous message, the length of that slice would be 0 - 7,
or -7, which obviously won't work very well!  Instead of bailing out
with an error there, Python just gives you the shortest substring it
can, ''.

[*] Btw, I've been using 's' instead of 'str' just because it's good
practice not to shadow builtin names like 'str'.
-- 
Zach
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Philosophical question about string slicing from end of a string

2014-11-24 Thread Steven D'Aprano
On Mon, Nov 24, 2014 at 12:32:27PM -0600, boB Stepp wrote:
 Python 2.7.8
 Win7Pro
 
  str = 0123456789
  str[-1]
 '9'
  str[-3:-1]
 '78'
  str[-3:]
 '789'
 
 I understand that the above is the way it is in Python, but I am
 puzzled why the designers did not choose that str[-3:-1] returns
 '789', especially since str[-1] returns '9'. What is the reason for
 choosing Python's actual behavior?

The slice indexes fall *between* characters, not on them. So the string 
abcdef will have indexes:

|a|b|c|d|e|f|
0 1 2 3 4 5 6

(Things may not line up perfectly unless you read this will a monospaced 
font like Courier.)

Think of slicing as cutting on the lines, so the slice [1:4] cuts just 
before b and just after d.

Negative indexes work the same way, just in the opposite direction:

 |a|b|c|d|e|f|
-6-5-4-3-2-1 ?

Note that the final line, marked with a question mark, would have been 0 
except that 0 is already used for positive slicing. 

So the slice [-3:-1] cuts before the d and just after e.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Philosophical question about string slicing from end of a string

2014-11-24 Thread eryksun
On Mon, Nov 24, 2014 at 1:33 PM, Zachary Ware
zachary.ware+py...@gmail.com wrote:
 Also note that there's no way to get the last member with a negative
 second index.

Also note that, given a -1 step, there's no way to get the first
member with a non-negative second index.

 s[-1:0:-1]
'987654321'

It requires a negative index that's at least one less than the
negative index of the first member:

 s[-1:-len(s)-1:-1]
'9876543210'

If possible, leave this implicit in the slice:

 s[::-1]
'9876543210'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor