Search substring in a string and get index of all occurances

Fredrik Lundh fredrik at pythonware.com
Wed Jun 21 17:00:32 CEST 2006


Tim Chase wrote:

>  >>> indicies = [i for i in xrange(len(mystring)) if mystring.startswith(substr, i)]
>  >>> indicies
> [4, 8, 10, 17, 22]
> 
> is my preferred way of doing this.

it's things like this that makes me wonder why I spent a week speeding 
up the string implementation for Python 2.5 (with special emphasis on 
find/index-related performance)...

</F>




More information about the Python-list mailing list