我想统计一个词出现在sting列表中的次数。从计数器对象提取词典
['this is a red ball','this is another red ball']
我写了下面的代码
counts = Counter()
for sentence in lines:
counts.update(word.strip('.,?!"\'').lower() for word in sentence.split())
它给了我下面的格式
Counter({'': 6, 'red': 2, 'this': 2, ....})
我怎么只能拿到字典的结果?
“Counter”是字典的一个子类 - 您可以对前者做任何事情,您可以对后者进行处理。你为什么要把它转换成一个香草字典?但很容易:'字典(计数)'。 – jonrsharpe