我正在编写一个程序,它采用我定义的两个函数并返回 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)