I have a string that looks like:
string = 'TTHHTHHTHHHHTTHHHTTT'
How can I count the number of runs in the string so that I get,
5 runs of T and 4 runs of H
You can use a combination of itertools.groupby and collections.Counter:
>>> from itertools import groupby
>>> from collections import Counter
>>> strs = 'TTHHTHHTHHHHTTHHHTTT'
>>> Counter(k for k, g in groupby(strs))
Counter({'T': 5, 'H': 4})
itertools.groupby groups the item based on a key.(by default key is the items in the iterable itself)
>>> from pprint import pprint
>>> pprint([(k, list(g)) for k, g in groupby(strs)])
[('T', ['T', 'T']),
 ('H', ['H', 'H']),
 ('T', ['T']),
 ('H', ['H', 'H']),
 ('T', ['T']),
 ('H', ['H', 'H', 'H', 'H']),
 ('T', ['T', 'T']),
 ('H', ['H', 'H', 'H']),
 ('T', ['T', 'T', 'T'])]
Here first item is the key(k) based on which the items were grouped and list(g) is the group related to that key. As we're only interested in key part, so, we can pass k to collections.Counter to get the desired answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With