I have this long string:
string = 'FFFFFFFFFFFFFFFFFFFFFABCABCABCBACBCABCBCABCABCBACBFFFFFACBCABCAFFF'
I want to count the occurrences of repeated 'F' sequences and their length. In this example, there would be 3 repeated instances of 'F', and the length of each instance would be 21, 5, and 3, respectively.
Using itertools.groupby
:
>>> string = 'FFFFFFFFFFFFFFFFFFFFFABCABCABCBACBCABCBCABCABCBACBFFFFFACBCABCAFFF'
>>> from itertools import groupby
>>> [len(list(group)) for char, group in groupby(string) if char == "F"]
[21, 5, 3]
groupby
takes any iterable and forms groups of equal items, yielding tuples of a single instance of the item value and a "group" iterable of the actual items.
The above iteration over a groupby
call gives you groups of equal letters, filters for those where the letter was F, and converts the groups to lists in order to be able to easily get their len
.