2013-03-08 46 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 

應該更好地工作......