[Python-ideas] What are the strong use cases for str.rindex()?
MRAB
python at mrabarnett.plus.com
Tue Apr 23 15:56:00 EDT 2019
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.
[snip]
More information about the Python-ideas
mailing list