[Python-ideas] What are the strong use cases for str.rindex()?

林自均 johnlinp at gmail.com
Tue Apr 23 20:59:18 EDT 2019

Hi all,

Thanks for the explanation. Now I agree that the need for list.rindex() is
not as common as str.rindex(). In fact, I only need list.rindex() when
doing some algorithm problems. I guess that doesn't count as real need here.

John Lin

Guido van Rossum <guido at python.org> 於 2019年4月24日 週三 上午4:20寫道:

> On Tue, Apr 23, 2019 at 1:02 PM MRAB <python at mrabarnett.plus.com> wrote:
>> On 2019-04-23 18:52, Terry Reedy wrote:
>> > On 4/23/2019 2:44 AM, 林自均 wrote:
>> >> Hi all,
>> >>
>> >> I found that there are str.index() and str.rindex(), but there is only
>> >> list.index() and no list.rindex().
>> >
>> > str.index and list.index are related but not the same.  The consistency
>> > argument is better applied to find-rfind, index-rindex,
>> > partition-rpartition, etc.
>> >
>> > It is much more common to process strings right to left or in both
>> > directions, than process lists right to left or in both directions.
>> > Moreover, lists have a reverse method, strings do not.
>> > ''.join(reversed(somestring)) is likely slower, especially if there many
>> > non-ascii chars.  Moreover, somestring.rindex(substring) would have to
>> > have both somestring and substring reversed when substring is more than
>> > one char.
>> >
>> You can reverse a string with somestring[::-1].
>> Personally, I'm not convinced by the "lists can be reversed" argument.
> Me neither, though for substring checks, reversing the string would be
> even more cumbersome (you'd have to reverse the query string too).
> My money is on "nobody uses this for lists".
> Some use cases for rindex() on strings that I found in a large codebase
> here include searching a pathname for the final slash, a list of
> comma-separated items for the last comma, a fully-qualified module name for
> the last period, and some ad-hoc parsing of other things. The "last
> separator" use cases are the most common and here rindex() sounds very
> useful.
> --
> --Guido van Rossum (python.org/~guido)
> *Pronouns: he/him/his **(why is my pronoun here?)*
> <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190424/f8c12381/attachment.html>

More information about the Python-ideas mailing list