Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: rfind bug ?

2 views
Skip to first unread message

Chris Rebert

unread,
Apr 21, 2010, 4:56:41 AM4/21/10
to Stef Mientki, pytho...@python.org
On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki <stef.m...@gmail.com> wrote:
> With the following code, I would expect a result of 5 !!
>
>>>> a= 'word1 word2 word3'
>>>> a.rfind(' ',7)
> 11
>
> Is this a bug ?

No. Don't you think someone would have found such an obvious bug by now?

You want regular str.find(), which searches from left-to-right:
>>>> a= 'word1 word2 word3'
>>> a.find(' ')
5

str.rfind() is a variant of str.find() that searches from
right-to-left. The "r" is for "right".

Cheers,
Chris
--
http://blog.rebertia.com

James Mills

unread,
Apr 21, 2010, 5:06:12 AM4/21/10
to Stef Mientki, python list
On Wed, Apr 21, 2010 at 6:51 PM, Stef Mientki <stef.m...@gmail.com> wrote:
>
> With the following code, I would expect a result of 5 !!
>
>>>> a= 'word1 word2 word3'
>>>> a.rfind(' ',7)
> 11
>
> Is this a bug ?

Python's documentation states:

| rfind(...)
| S.rfind(sub [,start [,end]]) -> int
|
| Return the highest index in S where substring sub is found,
| such that sub is contained within s[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Return -1 on failure.

"Return the highest index in S"

I haven't looked at python's source code for the
str object, but perhaps this is exactly what it's
algorithm does!

cheers
James

Peter Otten

unread,
Apr 21, 2010, 5:15:42 AM4/21/10
to
Chris Rebert wrote:

[didn't see the original message]

> On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki <stef.m...@gmail.com>
> wrote:
>> With the following code, I would expect a result of 5 !!
>>
>>>>> a= 'word1 word2 word3'
>>>>> a.rfind(' ',7)
>> 11
>>
>> Is this a bug ?
>
> No. Don't you think someone would have found such an obvious bug by now?

Indeed.

OP: you may be looking for

>>> a = "a bb ccc"
>>> a[::-1].find(" ")
3

Peter

Paul Rudin

unread,
Apr 21, 2010, 5:42:27 AM4/21/10
to
Peter Otten <__pet...@web.de> writes:


> OP: you may be looking for
>
>>>> a = "a bb ccc"
>>>> a[::-1].find(" ")
> 3


But you should be aware of the effeciency implications of doing
this. a[::-1] constructs a new list. It's probably faster to do e.g.:
len(a) - a.rfind(..) - 1

Peter Otten

unread,
Apr 21, 2010, 5:54:50 AM4/21/10
to
Paul Rudin wrote:

> Peter Otten <__pet...@web.de> writes:
>
>
>> OP: you may be looking for
>>
>>>>> a = "a bb ccc"
>>>>> a[::-1].find(" ")
>> 3
>
>
> But you should be aware of the effeciency implications of doing
> this. a[::-1] constructs a new list.

A new string, yes.

> It's probably faster to do e.g.:
> len(a) - a.rfind(..) - 1

Yes, especially if the string is "long":

$ python -m timeit -s'a = "word1 word2 word3"' 'a[::-1].rfind(" ")'
1000000 loops, best of 3: 0.834 usec per loop
$ python -m timeit -s'a = "word1 word2 word3"*100' 'a[::-1].rfind(" ")'
100000 loops, best of 3: 5.04 usec per loop

$ python -m timeit -s'a = "word1 word2 word3"' 'len(a)-a.rfind(" ")-1'
1000000 loops, best of 3: 0.587 usec per loop
$ python -m timeit -s'a = "word1 word2 word3"*100' 'len(a)-a.rfind(" ")-1'
1000000 loops, best of 3: 0.592 usec per loop

But be aware of the following difference:

>>> a[::-1].rfind("not there")
-1

>>> len(a) - a.rfind("not there") -1
8

Peter

Stef Mientki

unread,
Apr 21, 2010, 5:59:37 AM4/21/10
to pytho...@python.org
On 21-04-2010 10:56, Chris Rebert wrote:
> On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki <stef.m...@gmail.com> wrote:
>
>> With the following code, I would expect a result of 5 !!
>>
>>
>>>>> a= 'word1 word2 word3'
>>>>> a.rfind(' ',7)
>>>>>
>> 11
>>
>> Is this a bug ?
>>
> No. Don't you think someone would have found such an obvious bug by now?
>
if it's not a bug,
then the start index has no meaning ...
... and some would call that a bug.

cheers,
Stef

Jean-Michel Pichavant

unread,
Apr 21, 2010, 6:13:38 AM4/21/10
to Stef Mientki, pytho...@python.org
a.rfind(' ', 12)
Out[12]: -1

looks like the start index has a meaning ...

JM

Chris Rebert

unread,
Apr 21, 2010, 6:26:21 AM4/21/10
to Stef Mientki, pytho...@python.org
On Wed, Apr 21, 2010 at 2:59 AM, Stef Mientki <stef.m...@gmail.com> wrote:
> On 21-04-2010 10:56, Chris Rebert wrote:
>> On Wed, Apr 21, 2010 at 1:51 AM, Stef Mientki <stef.m...@gmail.com> wrote:
>>> With the following code, I would expect a result of 5 !!
>>>
>>>>>> a= 'word1 word2 word3'
>>>>>> a.rfind(' ',7)
>>>>>>
>>> 11
>>>
>>> Is this a bug ?
>>>
>> No. Don't you think someone would have found such an obvious bug by now?
>>
> if it's not a bug,
> then the start index has no meaning ...
> ... and some would call that a bug.

Ah, I neglected to take your use of .rfind()'s second parameter into account!

As can be interpolated from the part of the docs James quotes:
s.rfind(' ', 7) === s[7:].rfind(' ') + 7 # overlooking the 'not present' case

That is, the second parameter to .rfind(), namely `start`, is relative
to the *left* end of the string, not the right end. I can see how this
might be unintuitive, but it does make the API more uniform.

Cheers,
Chris

Alf P. Steinbach

unread,
Apr 21, 2010, 6:33:27 AM4/21/10
to
* Chris Rebert:

It seems that the OP also thought it was relative to the left end of the string.

The difference is what it signifies: start of search, or end of search.

With rfind the "start" parameter signifies the end of the search, and
conversely, the third parameter "end" signifies where the search starts. :-)


Cheers,

- Alf

Stef Mientki

unread,
Apr 21, 2010, 7:09:27 AM4/21/10
to pytho...@python.org
thanks Alf,
that's indeed what I was missing.

cheers,
Stef

>
> Cheers,
>
> - Alf

Chris Rebert

unread,
Apr 21, 2010, 7:11:58 AM4/21/10
to Alf P. Steinbach, pytho...@python.org

It really doesn't help that the example in question is kinda lousy
(particularly, it's symmetrical with respect to whitespace and has
only 2 spaces).
Case in point, my misinterpretation of the OP's misinterpretation
(i.e. having the indexing of `start` be relative to the end of the
string) still plausibly explains his confusion.

Cheers,
Chris
--
Excellent catch.
*Considers getting Alien Life-Form merch*

0 new messages