给定一个单词列表,求出这个列表中出现频次最高的 n 个单词
参考代码:
class Solution: def topKFrequentWords(self, words, k): dict = {} res = [] for word in words: if word not in dict: dict[word] = 1 else: dict[word] += 1 sorted_d = sorted(dict.items(), key = lambda x:x[1], reverse=True) for i in range(k): res.append(sorted_d[i][0]) return res