Count a group of same character in python

721 Views Asked by At

I have an input file like this:

CCCCCCCCCCCCCBBBCCBBBBBBBBBBBCCCCCCCCCCCCCCCCBCCC

I want to count how many 'B' are there on each group. So the output will be:

B: 3, 11, 1

I tried two different scripts but both give the total number of B = 15.

Here is one of my attempts:

import collections

with open('input.txt') as infile:  
    counts = collections.Counter(B.strip() for B in infile)  
for line, count in counts.most_common():  
    print line, count 
3

There are 3 best solutions below

2
On

Try this instead:

with open('input.txt') as infile:  
    counts = [i.count('B') for i in infile]

>>>print(counts)
 
[3, 11, 1]
1
On

This is a good application of itertools.groupby which will group like-valued inputs into a subiterator.

>>> import itertools
>>> text="CCCCCCCCCCCCCBBBCCBBBBBBBBBBBCCCCCCCCCCCCCCCCBCCC"
>>> b_counts = []
>>> for letter, repeats in itertools.groupby(text):
...     if letter == "B":
...             b_counts.append(len(list(repeats)))
... 
>>> b_counts
[3, 11, 1]
1
On

Seems simple enough.

def countBGroups(S):

    groups = []
    c = 0

    for s in S:

        if s == "B":
            c += 1
        else:
            if c != 0:
                groups.append(c)
            
            c = 0
    
    if c != 0:
        groups.append(c)
    
    return groups

with open("input.txt") as f:

    print(countBGroups(f.read()))