【发布时间】:2020-11-30 22:45:51
【问题描述】:
我有一个这样的输入文件:
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
【问题讨论】:
-
尝试使用
"B"而不是B。 -
我试过不行
标签: python line-count