【问题标题】:Word Frequency Analysis - TypeError: '>=' not supported between instances of 'list' and 'int'词频分析 - TypeError: 'list' 和 'int' 的实例之间不支持'>='
【发布时间】:2019-05-13 18:31:01
【问题描述】:

我正在 Jupyter Notebook 上使用此网站运行有关词频分析的代码:http://theautomatic.net/2017/10/12/word-frequency-analysis/ ...当我到达该过程的最后时,运行时出现以下错误:

TypeError:“list”和“int”的实例之间不支持“>=”。

基本上,我必须过滤掉至少 3 次未提及 Netflix 的文章。

article_to_freq = {article:freq for article, freq in 
                   article_to_freq.items() if freq >= 3}

错误似乎发生在此代码的第 2 行:article_to_freq.items() if freq >= 3}

如前所述,我不断得到:

TypeError: 'list' 和 'int' 的实例之间不支持 '>='

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 你能和我们分享你的代码吗?
  • 看起来 freq 是列表,您无法将列表与 int 进行比较。看看freq 实际包含什么。您可以通过(临时)将if freq >= 3 替换为print(freq) 来执行此操作,这将打印每个freq 的内容。
  • 如果只包含一个int(如[4]),则需要将if freq >= 3替换为if freq[0] >= 3
  • 请检查此问题并更新问题以提供更多详细信息。

标签: python python-3.x word-frequency


【解决方案1】:

我认为问题在于您将作为列表(数组)的“freq”与作为整数(数字)的 3 进行比较。解决方案是使用 len(freq) 将数组的长度与数字 3 进行比较,如下例所示:

#random example of the list
freq =[1,2,3,4,5,6,7]

#you use len() to get the length of the array
if len(freq) >= 3:
    print(freq) #or do what ever it is you want to do with it

希望这有帮助

【讨论】:

    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2022-07-03
    • 2020-09-07
    • 2020-11-13
    • 2021-01-15
    相关资源
    最近更新 更多