如何操作python 3.x中的嵌套字典? [英] How to operate on nested dictionary in python 3.x?

查看:389
本文介绍了如何操作python 3.x中的嵌套字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这个问题,你能解决这个难题吗?我们去!



我们在一个两级字典中表示一系列匹配的玩家数量如下:

  {'match1':{'player1':57,'player2':38},'match2':{'player3':9,'player1':42},'match3 ':{'player2':41,'player4':63,'player3':91}} 

每个比赛都由一个字符串来标识,每个玩家也是一样。分数都是整数。与比赛相关联的名称不是固定的(这里是match1,match2,match3),玩家的名字也不是。玩家不需要在所有比赛中记录得分。



定义一个Python函数 orangecap(d)该表单的字典d,并标识总分最高的玩家。您的功能应该返回一个(playername,topscore),其中playername是一个字符串,具有最高分数的播放器的名称,topscore是一个整数,playername的总分。



输入将与总分最高无关。



例如:

 >>> orangecap({'match1':{'player1':57,'player2':38},'match2':{'player3':9,'player1':42},'match3':{'player2' 'player4':63,'player3':91}})
('player3',100)

>>> orangecap({'test1':{'Ashwin':84,'Kohli':120},'test2':{'ashwin':59,'Pujara':42}})
('Kohli',120 )


解决方案

您可以轻松使用运行计数器来计数分数第一:

 从集合导入计数器

def orangecap(d):
total = Counter()
for d.values()中的match_result:
total.update(match_result)
返回total.most_common(1)[0]

这里 Counter.update(iterable) 将增加所提及的玩家的计数器。 Counter.most_common( n) 指定我们想要第一个最常见的元素。这将返回一个列表,我们选择第一个元组。

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


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:

{'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}}

Each match is identified by a string, as is each player. The scores are all integers. The names associated with the matches are not fixed (here they are 'match1','match2','match3'), nor are the names of the players. A player need not have a score recorded in all matches.

Define a Python function orangecap(d) that reads a dictionary d of this form and identifies the player with the highest total score. Your function should return a pair (playername,topscore) where playername is a string, the name of the player with the highest score, and topscore is an integer, the total score of playername.

The input will be such that there are never any ties for highest total score.

For instance:

>>> orangecap({'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}})
('player3', 100)

>>> orangecap({'test1':{'Ashwin':84, 'Kohli':120}, 'test2':{'ashwin':59, 'Pujara':42}})
('Kohli', 120)

解决方案

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)

这篇关于如何操作python 3.x中的嵌套字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆