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

前端 未结 3 604
予麋鹿
予麋鹿 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:46

    numbers = {'a': 1, 'b': 0, 'c': 1, 'd': 3, 'e': 3}
    max_value = max(numbers.values())
    
    
    [k for k,v in numbers.iteritems() if v == max_value]
    

    prints

     ['e', 'd']
    

    what it does is, loop over all entries via .iteritems and then check if the value is the maximum and if so add the key to a list.

提交回复
热议问题