python - python 列表中不太常见的单词

标签 python list counter

我已经统计了最常见的单词,只按顺序保留列表中 128 个最常见的单词:

words = my_list
mcommon_words = [word for word, word_count in Counter(words).most_common(128)]
my_list = [x for x in my_list if x in mcommon_words]
my_list = OrderedDict.fromkeys(my_list)
my_list = list(my_list.keys())

但现在我想用同样的方式统计 128 个不太常见的单词。更快的解决方案也会对我有很大帮助

最佳答案

most_common 返回单词及其计数作为元组列表。此外,if no argument is given, it returns all the words .

该方法返回一个列表,这意味着您可以使用切片来获取第一个和最后一个 n 元素。

演示:

l = list("asadfabsdfasodfjoasdffsafdsa")
sorted_items = [w for w, _ in Counter(l).most_common()]

print(sorted_items[:2])  ## Print top 2 items
print(sorted_items[-2:]) ## Print last 2 items

关于python - python 列表中不太常见的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49058353/

相关文章:

c# - 这可能使用泛型吗? C#

vb.net - 在 VB.NET 中对对象列表进行排序

python - 在 Python 中将计数器打印到 csv 文件错误

java - 计算字符串中逗号的数量,双引号之间的逗号除外

Python 无法导入名称

python - SGDRegressor 无意义的结果

java - 对多个列表中的元素进行分组

jQuery 倒计时暂停和重复

python - 正则表达式获取所有字母字符

python - Onnx 模型输入大小与 Opencv 帧大小不同