Bug in string.find
tjreedy at udel.edu
Fri Sep 2 04:33:17 CEST 2005
"Ron Adam" <rrr at ronadam.com> wrote in message
news:SXMRe.172$xl6.121 at tornado.tampabay.rr.com...
> Fredrik Lundh wrote:
>> Ron Adam wrote:
>>>The problem with negative index's are that positive index's are zero
>>>based, but negative index's are 1 based. Which leads to a non
>> indices point to the "gap" between items, not to the items themselves.
> So how do I express a -0?
You just did ;-) but I probably do not know what you mean.
> Which should point to the gap after the last item.
The slice index of the gap after the last item is len(seq).
>> straight indexing returns the item just to the right of the given gap
>> (this is
>> what gives you the perceived assymmetry), slices return all items
>> the given gaps.
> If this were symmetrical, then positive index's would return the value
> to the right and negative index's would return the value to the left.
As I posted before (but perhaps it arrived after you sent this), one number
indexing rounds down, introducing a slight asymmetry.
> Have you looked at negative steps? They also are not symmetrical.
> All of the following get the center 'd' from the string.
> a = 'abcdefg'
> print a # d 4 gaps from beginning
> print a[-4] # d 5 gaps from end
It is 3 and 4 gaps *from* the left and right end to the left side of the
'd'. You can also see the asymmetry as coming from rounding 3.5 and -3.5
down to 3 and down to -4.
> print a[3:4] # d
> print a[-4:-3] # d
These are is symmetric, as we claimed.
> print a[-4:4] # d
Here you count down past and up past the d.
> print a[3:-3] # d
Here you count up to and down to the d. The count is one more when you
cross the d than when you do not. You do different actions, you get
different counts. I would not recommend mixing up and down counting to a
beginner, and not down and up counting to anyone who did not absolutely
> print a[3:2:-1] # d These are symetric?!
> print a[-4:-5:-1] # d
> print a[3:-5:-1] # d
> print a[-4:2:-1] # d
The pattern seems to be: left-gap-index : farther-to-left-index : -1 is
somehow equivalent to left:right, but I never paid much attention to
strides and don't know the full rule.
Stride slices are really a different subject from two-gap slicing. They
were introduced in the early years of Python specificly and only for
Numerical Python. The rules were those needed specificly for Numerical
Python arrays. They was made valid for general sequence use only a few
years ago. I would say that they are only for careful mid-level to expert
use by those who actually need them for their code.
Terry J. Reedy
More information about the Python-list