我有一个txt文件,从中我想要计算每个
字的频率,之后,我想排序列表和排序后我想要
打印频率按相关词汇从大到小排序。我
编写Python代码,但我不记得如何做到这一点。代码是
如何找到一个字aftr排序inthon的频率
frequency = []
file = open("C:/Python26/rzlt.txt");
contents=file.read();
tokens = nltk.word_tokenize(contents);
f=open("frequencies.txt",'w')
f2=open("count.txt",'w')
for t in tokens:
freq = str(tokens.count(t))
frequency.append(freq)
f.write(t+"\t"+freq)
frequency.sort(reverse=True)
for t in tokens:
f2.write(t+"\t"+ frequency(t))
f.close()
f2.close()
你为什么使用nltk? – 2015-04-03 10:24:44
我知道它没有必要 – 2015-04-03 10:26:54
实际问题是在最后一个循环通过我想保存下降列表的频率与相关的令牌 – 2015-04-03 10:31:25