【问题标题】:NLTK - how to get items where the frequency distribution is greater than a specific numberNLTK - 如何获取频率分布大于特定数字的项目
【发布时间】:2021-02-11 23:19:16
【问题描述】:

如果列表超过一定数量,我正在尝试获取列表的频率分布。

例子:

import nltk
test_list=['aa', 'aa', 'bb', 'cc', 'dd', 'dd']
test_fd = nltk.FreqDist(test_list)

返回:

FreqDist({'aa': 2, 'dd': 2, 'bb': 1, 'cc': 1})

没有循环,我正在寻找所有大于 1 的项目。

使用 Python 3.8 和 NLTK 3.5

【问题讨论】:

标签: python-3.x nltk


【解决方案1】:

这是一个可能的解决方案:

test_fd = nltk.FreqDist({k: v for k, v in test_fd.items() if v > 1})

【讨论】:

    【解决方案2】:

    可以使用filter 完成,您可以决定将dictlist(元组)作为输出:

    test_fd = dict(filter(lambda x: x[1] > 1, nltk.FreqDist(test_list).items()))
    

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 2012-04-19
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多