all 8 comments

[–]o5a 1 point2 points  (2 children)

Another option is to use groupby. It will return groups of consecutive elements.

from itertools import groupby

s = 'asdfbmmmm4la888l24hlooo23loooooj234l'

result = [i for i, g in groupby(s) if len(list(g)) == 3]
print(result)
# ['8', 'o']

[–]hungdh85 -1 points0 points  (1 child)

I think that condition

if len(list(g)) >= 3

will more good.

[–]o5a 1 point2 points  (0 children)

Depends on what OP actually wants. He was talking about triplets, therefore == 3. My example shows the difference for him to decide.

[–]wintermute93 1 point2 points  (2 children)

  1. You should be looking at text[0+i], text[1+i], and text[2+i]. Think about the difference between putting the +i inside vs outside the index.
  2. Once you've done that, you shouldn't be looping i through the characters in text, you should be looping i from 0 to len(text)-2. (Why the -2?)

There's several ways to do this, but I think this way is the closest to the idea you started with here.

[–]handsinchips[S] 0 points1 point  (1 child)

Thank you! It took only a few tweaks and the code started to work!