我有一个这样的输入文件:
CCCCCCCCCCCCCBBBCCBBBBBBBBBBBCCCCCCCCCCCCCCCCBCCC
我想计算每组有多少个“B”。所以输出将是:
B: 3, 11, 1
我尝试了两个不同的脚本,但都给出了 B = 15 的总数。
这是我的尝试之一:
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