Using Python's max to return two equally large values

前端 未结 6 1880
小鲜肉
小鲜肉 2020-11-30 13:29

I\'m using Python\'s max function to find the largest integer in a dictionary called count, and the corresponding key (not quite sure if I\'m saying it properly

6条回答
  •  没有蜡笔的小新
    2020-11-30 14:20

    Same idea as Asterisk, but without iterating over the list twice. Bit more verbose.

    count = { 'a': 120, 'b': 120, 'c': 100 }
    answers = []
    highest = -1
    
    def f(x):
        global highest, answers
        if count[x] > highest:
            highest = count[x]
            answers = [x]
        elif count[x] == highest:
            answers.append(x)
    
    map(f, count.keys())
    print answers
    

提交回复
热议问题