你的错误是
inp = input()[:-1]
[:-1]
切掉这个词的最后一个字符。我想你是想删除换行符,但是
input()
已经是了“
stripping a trailing newline
>>> [input() for _ in range(2)]
foo
bar
['foo', 'bar']
更简单的解决方案,顺便说一句:
from collections import Counter
ctr = Counter(input() for _ in range(int(input())))
print(len(ctr))
print(*ctr.values())
from collections import Counter
ctr = Counter(map(input, [''] * int(input())))
print(len(ctr))
print(*ctr.values())
另一个:
from collections import Counter
import sys
next(sys.stdin)
ctr = Counter(map(str.strip, sys.stdin))
print(len(ctr))
print(*ctr.values())
这一个读整行,所以这里的字符串
做
包括换行符。那没关系,如果
全部的
strip
每一行都有。叹息。