def nMostCommonWords(n, file): """ returns a list of the n most common words """ contents = open(file).read() words = re.findall('\w+', contents, re.M) words = [x.lower() for x in words] wordsDict = dict() for word in words: if word not in wordsDict: wordsDict[word] = 1 else: wordsDict[word] += 1 wordCount = wordsDict.items() wordCount.sort(cmp = lambda x,y: y[1] - x[1]) return wordCount[0:n]