'What does "count.get" mean in Leetcode 347. Top K Frequent Elements
In the solution of the problem (Leet code 347. Top K Frequent Elements)
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
from collections import Counter
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
if k == len(nums):
return nums
count = Counter(nums)
return heapq.nlargest(k, count.keys(), key=count.get)
I cannot understand what
key=count.get
means at the last line.
When the input is
nums = [1,1,1,2,2,3], k = 2
the answer should be
[1,2]
If I cut
key=count.get
from the last line, the output would be
[3,2]
Could anyone teach me what "count.get" does?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
