我想找到在给定文件中出现过n次(例如200次)的单词列表。为此,我使用以下代码获取文件中的每个唯一标记,但我不明白如何在出现n次的情况下获取这些唯一标记。
from collections import Counter
import re
seen = list()
words = re.findall(r'[\w+]+', open('deneme.txt').read())
seen = Counter(words).most_common()
输出为:
[('Erke', 4), ('aç+Noun', 4), ('Antalya', 3), ('123', 3), ('ol+Verb', 3), ('Varol', 2), ('Koleji', 1), ('asdfsdf', 1), ('birak+Verb', 1)]
例如,我想获得3次的令牌。我该如何实现。我无法达到列表中的出现次数。
答案 0 :(得分:5)
您可以使用list comprehension:
from collections import Counter
import re
seen = list()
words = re.findall(r'[\w+]+', open('deneme.txt').read())
seen = Counter(words).most_common()
print([w for w, c in seen if c == 3])
输出
['123', 'Antalya', 'ol+Verb']