python 中的词典排序不正确
Dictionary not sorting correctly in python
我的代码应该输出语料库中频率最高的前 10 个词。但是,它给出了 10 个随机单词的输出。
from nltk.corpus import brown
import operator
brown_tagged_sentences = brown.tagged_sents(categories='news')
fd=nltk.FreqDist(brown.words(categories='news'))
sorted_fd = dict(sorted(fd.items(), key=operator.itemgetter(1), reverse=True))
print(sorted_fd)
most_freq_words=list(sorted_fd)[:10]
for word in most_freq_words:
print(word,':',sorted_fd[word])
当前输出如下错误:
Rae : 1
discharge : 1
ignition : 1
contendere : 1
done : 24
meaning : 4
ashore : 1
Francesca : 1
Vietnamese : 1
data : 4
请帮忙
有解决办法。希望这是最好的方法:
fd=nltk.FreqDist(brown.words(categories='news'))
sorted_fd = sorted(fd.items(), key=operator.itemgetter(1), reverse=True)
most_freq_words = [w for w,c in sorted_fd]
most_freq_words = most_freq_words[:10]
for word in most_freq_words:
print(word,':',fd[word])
nltk的FreqDist()
class可以直接给你它的内容,使用频率降序排列,使用方法most_common()
:
fd=nltk.FreqDist(brown.words(categories='news'))
for w, f in fd.most_common(10):
print(w+":", f)
我的代码应该输出语料库中频率最高的前 10 个词。但是,它给出了 10 个随机单词的输出。
from nltk.corpus import brown
import operator
brown_tagged_sentences = brown.tagged_sents(categories='news')
fd=nltk.FreqDist(brown.words(categories='news'))
sorted_fd = dict(sorted(fd.items(), key=operator.itemgetter(1), reverse=True))
print(sorted_fd)
most_freq_words=list(sorted_fd)[:10]
for word in most_freq_words:
print(word,':',sorted_fd[word])
当前输出如下错误:
Rae : 1
discharge : 1
ignition : 1
contendere : 1
done : 24
meaning : 4
ashore : 1
Francesca : 1
Vietnamese : 1
data : 4
请帮忙
有解决办法。希望这是最好的方法:
fd=nltk.FreqDist(brown.words(categories='news'))
sorted_fd = sorted(fd.items(), key=operator.itemgetter(1), reverse=True)
most_freq_words = [w for w,c in sorted_fd]
most_freq_words = most_freq_words[:10]
for word in most_freq_words:
print(word,':',fd[word])
nltk的FreqDist()
class可以直接给你它的内容,使用频率降序排列,使用方法most_common()
:
fd=nltk.FreqDist(brown.words(categories='news'))
for w, f in fd.most_common(10):
print(w+":", f)