You can achieve this with itertools:
import itertools
s = "TTTHTHTTHTTTTHTTTHTTTTTTHTTTTTHTH"
counts = []
count = 1
for a, b in zip(s, s[1:]):
if a==b:
count += 1
elif a == "T":
counts.append(count)
count = 1
gives:
counts
=> [3, 1, 2, 4, 3, 6, 5, 1]
source
share