Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting runs in a string

Tags:

python

string

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

like image 467
coderkid Avatar asked Sep 06 '13 19:09

coderkid


1 Answers

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.

like image 167
Ashwini Chaudhary Avatar answered Oct 13 '22 00:10

Ashwini Chaudhary