-2

I have a text:

joiskodposkfkfsdpsstopjisjijdjnsndsjijdstopisjidinsindistopskndishf

which contains several "stop". I would like to find the index for all locations where "stop" occur. How can I do this? I have tried list but I only seem to get the first location then. Can you use enumerate for this?

Thanks!

Mazdak
  • 105,000
  • 18
  • 159
  • 188
HVO
  • 1
  • 2

2 Answers2

2

You can use a regular expression:

import re
for match in re.finditer('stop', text):
    print match.start()

https://docs.python.org/2/library/re.html#re.finditer

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • What if the matches were overlapping? I would still like to find them. – HVO Nov 10 '14 at 11:21
  • Then you can use `re.RegexObject.search()` (https://docs.python.org/2/library/re.html#re.RegexObject.search) with the `pos` parameter to specify the start location. Do one search, then another from one character after the result from the first search, and so on until you find no more matches. – John Zwinck Nov 11 '14 at 00:11
1

Remark: Use the re.finditer() solution instead! It is more efficient and more readable.


You can simply iterate over the string and check if the string, starting at the given character, starts with 'stop' word:

>>> s = 'joiskodposkfkfsdpsstopjisjijdjnsndsjijdstopisjidinsindistopskndishf'
>>> [pos for pos in range(len(s)) if s[pos:].startswith('stop')]
[18, 39, 55]

Another option would be repeatedly calling s.find('stop', X) with X being the position of the last match + 1 (or 0 if you never had a match).

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    Get Slice ( 's[pos:]' ) is not O(1), so this solution is not as good as John Zwinck's (with the given text it is 7 times slower on my computer and becoming worse with longer text) – espang Nov 09 '14 at 11:12
  • True. Already upvoted @JohnZwinck's. Added a notice here to make it clearer. – ThiefMaster Nov 09 '14 at 12:14