【发布时间】:2017-08-17 08:51:46
【问题描述】:
我想通过以下条件从列表中筛选出一些令牌。 1) 令牌长度大于 5 2)出现频率(原文中)超过100
我使用了以下代码
#token_list is a list object containing tokenized words from raw text
from collections import Counter
c = Counter(token_list)
selected_tokens = [word for word in token_list if len(word) > 5 and c.item[2] > 100]
selected_tokens
但似乎无法得到它。我相信错误来自“c.item[2]”,但不太了解“Counter()”命令背后的机制。
如果有人能就此启发我,将不胜感激。
谢谢。
【问题讨论】:
-
从字面上看,the
Counterdocumentation 中的第一句话告诉你如何使用它们:“A Counter is a dict subclass”。如果要使用类或函数,最好先阅读其文档。