-2

given a long string, for example: str = 'abbabaab'

and a short substring: sub = 'ab'.

I want to get a list of all the indexes where the substring can be found, without iterating over the string.

the expected result would be: res = [0, 3, 6]

Zusman
  • 606
  • 1
  • 7
  • 31

1 Answers1

1

Coming from here:

import re

str = 'abbabaab'
sub = 'ab'

res = [x.start() for x in re.finditer(sub, str)]
print(res)                                            # [0, 3, 6]
DirtyBit
  • 16,613
  • 4
  • 34
  • 55