gpt4 book ai didi

python - 如何计算多个标记化单词列表中最常见的 10 个单词

转载 作者:行者123 更新时间:2023-11-28 22:11:06 25 4
gpt4 key购买 nike

我有一个数据集,其中包含大量标记化单词列表。例如:

['apple','banana','tomato']
['tomato','tree','pikachu']

我有大约 40,000 个这样的列表,我想统计所有 40,000 个列表中最常见的 10 个单词。

有人知道吗?

最佳答案

您可以使用 itertools.chain 展平嵌套列表并使用 Counter 获取最常用的词及其 most_common 方法:

from itertools import chain
from collections import Counter

l = ['apple','banana','tomato'],['tomato','tree','pikachu']

Counter(chain(*l)).most_common(10)
# [('tomato', 2), ('apple', 1), ('banana', 1), ('tree', 1), ('pikachu', 1)]

关于python - 如何计算多个标记化单词列表中最常见的 10 个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com