How to get all the keys with the same highest value?

前端 未结 3 592
予麋鹿
予麋鹿 2020-12-16 01:11

If I have a dictionary with their corresponding frequency values:

numbers = {a: 1, b: 4, c: 1, d: 3, e: 3}

To find the highest, what I know

3条回答
  •  [愿得一人]
    2020-12-16 01:38

    The collections.Counter object is useful for this as well. It gives you a .most_common() method which will given you the keys and counts of all available values:

    from collections import Counter
    numbers = Counter({'a': 1, 'b': 0, 'c': 1, 'd': 3, 'e': 3})
    values = list(numbers.values())
    max_value = max(values)
    count = values.count(max_value)
    numbers.most_common(n=count)
    

提交回复
热议问题