繁体   English   中英

返回彼此对应的两个列表?

[英]returning two lists that correspond with each other?

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

应该更好地工作...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM