I'm writing a program that takes two functions I have defined and returns n number of words and their frequencies. The problem is, my program is only returning the frequencies. I've tried zipping the words and frequencies together but that hasn't worked. From what you see, am I approaching this wrong?
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)
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
should work better ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.