How to operate on nested dictionary in python 3.x?

后端 未结 5 707
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 14:47

I am stuck with this question, can you solve the challenge? Here we go!

We represent scores of players across a sequence of matches in a two level dictionary as follows:

5条回答
  •  隐瞒了意图╮
    2021-01-29 15:28

    You can easily use a running counter to count the scores first:

    from collections import Counter
    
    def orangecap(d):
        total = Counter()
        for match_result in d.values():
            total.update(match_result)
        return total.most_common(1)[0]
    

    Here Counter.update(iterable) will increment the counters for the players mentioned. Counter.most_common(n) specifies we want the first most common element. This will return a list and we pick the first tuple.

    >>> orangecap(d)
    ('player3', 100)
    

提交回复
热议问题