2013-03-08 48 views
0

我正在编写一个程序,它具有我定义的两个函数,并返回n个单词及其频率。问题是,我的程序只返回频率。我试过一起压缩文字和频率,但是没有奏效。从你看到的,我接近这个错误?返回两个对应的列表?

def computeWordFrequencies(filename): #my function 
    f = open(filename,'r') 
    j = f.read() 
    OriginalL1 = parse(j) 
    L1 = unique(parse(j)) 
    L2 = [OriginalL1.count(freq) for freq in L1] 
    L = [L1, L2] 
    return L 


def mostFrequentWords(word,frequency,n): 
    words = word 
    freqs = sorted(frequency,reverse=True) 
    return freqs[:n] 

L = computeWordFrequencies('file.txt') #takes a file and returns words & their frequencies 
words = zip(*sorted(zip(L[0],L[1]))) 
freqs = L[1] 
print mostFrequentWords(words,freqs,100) 
+0

做'computeWordFrequencies'和'file.txt'是什么样的? – Blender 2013-03-08 00:41:00

+0

我将computeWordFrequencies添加到问题中,file.txt是我在computeWordFrequencies中放置的任何文本 – user2041448 2013-03-08 00:56:00

回答

1
def mostFrequentWords(word,frequency,n): 
    my_list = zip(word,frequency) #combine the two lists 
    my_list.sort(key=lambda x:x[1],reverse=True) #sort by freq 
    words,freqs = zip(*my_list[:n]) #take the top n entries and split back to seperate lists 
    return words #return our most frequent words in order 

应该更好地工作......