我正在编写一个程序,它接受我定义的两个函数,并返回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 :(得分: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
应该更好......