2015-04-03 33 views
0

我有一个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() 
+0

你为什么使用nltk? – 2015-04-03 10:24:44

+0

我知道它没有必要 – 2015-04-03 10:26:54

+0

实际问题是在最后一个循环通过我想保存下降列表的频率与相关的令牌 – 2015-04-03 10:31:25

回答

1

with open() as .. :自动关闭文件。 collections.Counter()统计列表中的所有单词。

最后sorted()按降序排列Counter()对象。

import collections 

with open('my_text_file.txt', 'r') as f: 

    f_as_lst = f.read().split() 
    c = collections.Counter(f_as_lst) 

# Creates a list of tuples with values and keys swapped 
freq_lst = [(v, k) for k, v in c.items()] 
# Sorts list by frequency 
freq_lst = sorted(freq_lst, key=lambda item: item[0]) 

print freq_lst 

如果你不能使用collections.Counter(),可以使用下面的函数来替代它:

def my_counter(list_of_strings):  
    dct = {} 

    for string in list_of_strings: 
     if string not in dct: 
      dct.update({string: 1}) 
     else: 
      dct[string] += 1 

    return dct 
+0

AttributeError问题的原因:'模块'对象没有属性'Counter' – 2015-04-03 10:57:11

+0

@ShaheenGul它在我的文件中工作得很好。你确定你正确地复制了代码吗?此外,在这里显示完整的错误,以便我可以检查出来。 – 2015-04-03 10:58:53

+0

我使用python版本2.6,它不会导入计数器() – 2015-04-03 12:41:17

1

尝试这样的:使用计数器

import nltk 
from collections import Counter 
file = open("C:/Python26/rzlt.txt"); 
contents = file.read(); 
tokens = nltk.word_tokenize(contents); 
words = map(str.isalnum, tokens) 
frequency = Counter(words) 

for x, y in sorted(frequency.items(), key=lambda x:x[1]): 
    print x, y 
+0

ImportError:无法导入名称计数器问题 – 2015-04-03 11:00:05

+0

您怀疑哪个版本的python? – Hackaholic 2015-04-03 11:01:13

1
Try This, I had used collections for getting the count of the each word,
and for displaying it in ascending ordered i used sorted with parameter
reverse=True

import collections ## import the collection module
file = open("filename.txt") ## open the file which need to be sorted
list = [] ## Create the empty list
print "sorted data : "
print "==============================================="
for data in file: ## Iterate the data file
    list.append(data.strip())
print "\n".join(sorted(list)) ## Print each read line on next line
count = collections.Counter(list) ## Get the count of the each word
print "==============================================="
print "Count of each word is:"
for data in sorted(count, reverse=True): ## Iterate the file in ascending order
    print '%s : %d' % (data, count[data]) ## Print the read file in ascending order
+0

我使用python版本2.6,它不会导入计数器并导致问题。我上面的代码是准确的,只有问题是显示与相关令牌的频率(反向排序)。建议我的这个问题的解决方案只有 – 2015-04-03 12:39:39

+0

建议只有一个解决方案,将与他们的相关令牌显示频率。在我的代码中,txt文件的标记位于标记列表中,频率包含已排序的值。只有问题是在他们的显示 – 2015-04-03 12:47:35

+0

我可以知道你为什么使用nltk ???它的具体用途是什么?上述建议的解决方案,你看到工作或不? – abhijeetmote 2015-04-03 13:11:56

相关问题