find all index positions

Peter Otten __peter__ at web.de
Thu May 11 12:26:05 EDT 2006


alisonken1 wrote:

> ==========
> def getAllIndex(aString=None, aSub=None):
>     t=dict()
>     c=0
>     ndx=0
>     while True:
>         try:
>             ndx=aString.index(aSub, ndx)
>             t[c]=ndx
>             ndx += 1
>             c += 1
>         except ValueError:
>             break
>     return t
> ===========
> 
> This will return a dictionary of what was found; i.e.,
> 
>>>> getAllIndex('abcd 1234 efgh 1234 ijkl', '1234')
> {0: 5, 1: 15}

Consecutive integers starting at 0 as keys? Looks like you want a list
instead of a dict.

Peter



More information about the Python-list mailing list