Python3

https://leetcode.com/problems/largest-values-from-labels/

class Solution:
      def largestValsFromLabels(self, values: List[int], labels: List[int], num_wanted: int, use_limit: int) -> int:
        vl = list(zip(values,labels))
        vl = sorted(vl,key = lambda x:x[0],reverse = True)
        lt = {}
        ans = []
        for i in vl:
            if len(ans) >= num_wanted:
                return sum(ans)
 
            if i[1] in lt:
                if lt[i[1]] < use_limit:
                    ans.append(i[0])
                    lt[i[1]] +=1
                else:
                    continue
            else:
                ans.append(i[0])
                lt[i[1]] = 1
        return sum(ans)