【问题标题】:Python - Return top 5 words with highest frequencyPython - 返回频率最高的前 5 个单词
【发布时间】:2015-12-09 08:37:37
【问题描述】:

正如标题所说,我需要编写一个代码来返回频率最高的 5 个单词(来自输入字符串)的列表。这是我目前所拥有的:

from collections import defaultdict

def top5_words(text):
  tally = defaultdict(int)
  words = text.split()

  for word in words:
    if word in tally:
      tally[word] += 1
    else:
      tally[word] = 1

  answer = sorted(tally, key=tally.get, reverse = True)

  return(answer)

例如,如果你输入:top5_words("one one was a racehorse two two is one too") 它应该返回:["one", "two", "was", "a", "racehorse"] 但是而是返回:['one', 'was', 'two', 'racehorse', 'too', 'a'] - 有人知道这是为什么吗?

编辑:

感谢 Anand S Kumar,这就是我现在所拥有的:

import collections

def top5_words(text):

  counts =  collections.Counter(text.split())

  return [elem for elem, _ in sorted(counts.most_common(),key=lambda x:(-x[1], x[0]))[:5]]

【问题讨论】:

  • 字典没有任何顺序,对于具有相同计数的单词,顺序可以是任何东西。另外,您的预期输出对我来说没有任何意义。
  • 出场次数-一:3,曾:2,二:2,赛马:1,太:1,一:1。看来你需要按字母顺序打平。
  • 我该怎么做?

标签: python sorting dictionary frequency


【解决方案1】:

你应该使用 collections.Counter 然后你可以使用它的方法 - most_common() 。示例 -

import collections
def top5_words(text):
    counts = collections.Counter(text.split())
    return counts.most_common(5)

请注意,上面返回一个包含 5 个元组的列表,在每个元组中,第一个元素是实际单词,第二个元素是该单词的计数。

演示 -

>>> import collections
>>> def top5_words(text):
...     counts = collections.Counter(text.split())
...     return counts.most_common(5)
...
>>> top5_words("""As the title says, I need to write a code that returns a list of 5 words (from an input string) that have the highest frequency. This is what I have so far""")
[('that', 2), ('a', 2), ('I', 2), ('the', 2), ('have', 2)]

如果您只想要元素而不是 count ,那么您还可以使用列表推导来获取该信息。示例 -

import collections
def top5_words(text):
    counts = collections.Counter(text.split())
    return [elem for elem, _ in counts.most_common(5)]

演示 -

>>> import collections
>>> def top5_words(text):
...     counts = collections.Counter(text.split())
...     return [elem for elem, _ in counts.most_common(5)]
...
>>> top5_words("""As the title says, I need to write a code that returns a list of 5 words (from an input string) that have the highest frequency. This is what I have so far""")
['that', 'a', 'I', 'the', 'have']

对于来自 cmets 的新要求 -

对于频率相同的单词似乎仍然存在问题,我如何让它按字母顺序对频率相同的单词进行排序?

您可以首先获取所有单词及其计数的列表,然后使用sorted,这样 sorted 首先对计数进行排序,然后对元素本身进行排序(因此当计数相同时,它会按字典顺序排序)。示例 -

import collections
def top5_words(text):
    counts = collections.Counter(text.lower().split())
    return [elem for elem, _ in sorted(counts.most_common(),key=lambda x:(-x[1], x[0]))[:5]]

演示 -

>>> import collections
>>> def top5_words(text):
...     counts = collections.Counter(text.lower().split())
...     return [elem for elem, _ in sorted(counts.most_common(),key=lambda x:(-x[1], x[0]))[:5]]
...
>>> top5_words("""As the title says, I need to write a code that returns a list of 5 words (from an input string) that have the highest frequency. This is what I have so far""")
['a', 'have', 'i', 'that', 'the']

【讨论】:

  • 效果几乎*完美 - 当涉及相同频率的单词时似乎仍然存在问题,我如何让它按字母顺序对相同频率的单词进行排序?
  • 很高兴我能提供帮助。 :)
猜你喜欢
  • 2023-03-14
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 2020-04-02
  • 2015-07-01
相关资源
最近更新 更多