find all index positions
alisonken1
alisonken1 at gmail.com
Thu May 11 12:04:07 EDT 2006
micklee74 at hotmail.com wrote:
> hi
> say i have string like this
> astring = 'abcd efgd 1234 fsdf gfds abcde 1234'
> if i want to find which postion is 1234, how can i achieve this...? i
> want to use index() but it only give me the first occurence. I want to
> know the positions of both "1234"
> thanks
==========
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}
More information about the Python-list
mailing list