Bug in string.find

Ron Adam rrr at ronadam.com
Fri Sep 2 02:23:14 CEST 2005


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
>>symmetrical situations.
> 
> 
> indices point to the "gap" between items, not to the items themselves.

So how do I express a -0?  Which should point to the gap after the last 
item.


> 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 between
> 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.

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[3]         # d   4 gaps from beginning
print a[-4]        # d   5 gaps from end
print a[3:4]       # d
print a[-4:-3]     # d
print a[-4:4]      # d
print a[3:-3]      # d
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

This is why it confuses so many people.  It's a shame too, because slice 
objects could be so much more useful for indirectly accessing list 
ranges. But I think this needs to be fixed first.

Cheers,
Ron

















More information about the Python-list mailing list