def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
dict = {}
result = []
for i in range(len(nums)):
if dict.get(nums[i]) == None:
dict[nums[i]] = 1
else:
dict[nums[i]] += 1
dict = sorted(dict.items(), key=lambda d: d[1],reverse = True)
for j in range(0,k):
if k<1:
break
result.append(dict[j][0])
return result
建立字典,然后对字典按照value进行逆序排序
参考
1 python字典按照value进行排序 https://www.cnblogs.com/timtike/p/6562402.html