如何在Python中以单词列表的形式提取文本中出现频率最高的前10个单词?

huangapple go评论64阅读模式
英文:

How to extract the 10 most frequent words in a text in form of a list of words in Python?

问题

这是您需要的翻译部分:

这是我写的代码
```py
tuple(map(str, Counter(text).most_common(10)))

这是我得到的输出:

('science', 55)

这是我需要的输出:

["science"]
英文:

I have a text and am trying to extract the 10 most frequent words in it. I use the text.most_common(10) method, but am getting the ouput in form of a tuple which also contains the number of occurencies (which I don't need...). How can I fix this so that the output is just the words in form of a list?

Note: I can't use the nltk library in the program to be created.

this is the code I wrote:

tuple(map(str, Counter(text).most_common(10)))

this is the output I am getting:

('science', 55)

this is the output I need:

["science"]

答案1

得分: 1

[t[0] for t in counter.most_common(10)]

英文:

You need to get the first item in the pairs returned by Counter.most_common().

[t[0] for t in counter.most_common(10)]

Full demo:

from collections import Counter

text = """\
A Counter is a dict subclass for counting hashable objects. It is a collection
where elements are stored as dictionary keys and their counts are stored as
dictionary values. Counts are allowed to be any integer value including zero or
negative counts. The Counter class is similar to bags or multisets in other
languages.

Elements are counted from an iterable or initialized from another mapping (or
counter):

Counter objects have a dictionary interface except that they return a zero
count for missing items instead of raising a KeyError:

Setting a count to zero does not remove an element from a counter. Use del to
remove it entirely:

New in version 3.1.

Changed in version 3.7: As a dict subclass, Counter inherited the capability to
remember insertion order. Math operations on Counter objects also preserve
order. Results are ordered according to when an element is first encountered in
the left operand and then by the order encountered in the right operand.
"""

counter = Counter(text.split())

[t[0] for t in counter.most_common(10)]

gives

['a', 'to', 'Counter', 'are', 'in', 'is', 'the', 'dictionary', 'zero', 'or']

huangapple
  • 本文由 发表于 2023年1月9日 06:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定