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)
4

1 回答 1

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

应该工作得更好......

于 2013-03-08T00:42:27.610 回答