I needed a version of the string.index(sub) function which returns a list of indices of ALL occurances of a substring in the string.
Is there a better/shorter/more efficient way to do this? Please share.
1 2 3 4 5 6 7 8 9 | def allindices(string, sub, listindex, offset):
#call as l = allindices(string, sub, [], 0)
if (string.find(sub) == -1):
return listindex
else:
offset = string.index(sub)+offset
listindex.append(offset)
string = string[(string.index(sub)+1):]
return allindices(string, sub, listindex, offset+1)
|
this can be used to do string.replaceAll(sub1, sub2) sort of thing.
Tags: text
non-recursive. How about:
I prefer non-recursive functions. Also, there is no need to copy the string, because find() and index() can search from an offset themselves.
No Need to Pass in Empty List or Offset. How about :
def allindices(string, sub, offset=0, listindex=None): #call as l = allindices(string, sub) if listindex is None: listindex = [] if (string.find(sub) == -1): return listindex else: offset = string.index(sub)+offset listindex.append(offset) string = string[(string.index(sub)+1):] return allindices(string, sub, listindex, offset+1)
Don't reinvent the wheel. You should look at the "re" module in the standard library. To find all of the the starting positions of S in T:
Plus it supports substitutions, and lots of other goodness.
finditer() won't find overlapping strings. The regular expression methods, including finditer(), find non-overlapping matches. To find overlapping matches you need a loop.