查找嵌套字典值中值的总和 [英] Find the sum of values within the values of a nested Dictionary

查看:212
本文介绍了查找嵌套字典值中值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基于嵌套的字典的问题。



我们给了一个嵌套字典,其中在外部字典中,匹配被提及,匹配的值是另一个字母,它的关键字和值分别是这个函数的名称是 orangecap(d),它接受以下格式的字典



以下是示例。

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

所以我想通过玩家键搜索并计算总数所以输出需要是('player3',100)



这是我迄今为止所尝试的,但无效:

  def orangecap(d):
total = 0
为key,value in d.items():
d.items()中的值:
如果d.keys()中的d [key]:
total = total + d [value]
return(d [key],max(total))


解决方案

p>这是稍后修改的答案来自 我的 / strong>。

  def find_totals(d):
total = {}
用于匹配,结果在d.items()中:
为玩家,score.items()中的score:
total [player] = total.get(player,0)+ score
highest_score = max ,key = total.get)
return highest_score,total [highest_score]

示例输出:

 >>> d = {'match1':{'player1':57,'player2':38},'match2':{'player3':9,'player1':42},'match3':{'player2' 'player4':63,'player3':91}} 
>>>打印find_totals(d)
('player3',100)






那么你的代码怎么了,我们来研究一下算法:



首先,你遍历项目( keys / values d 。这很好,因为你试图遍历一个嵌套的字典结构。但是,使用第二个循环(迭代 而不是遍历最内部结构, ,而不是 d ),而是再次遍历 d



value 现在只是一个键/值 strong>存储在 d 中,而不是嵌套字典。 d [key] 只是映射到匹配键的值。那么如何才能在中列出密钥 - > d.keys()您的如果条件从不评估为 true 。尽管如此,只有在两个迭代之后,您才能将整个迭代的短路 返回语句。哪个都不返回正确的播放器( d [key] 是嵌套字典)和 max 采用可迭代的参数,而不是int。



您应该了解有关基本控制流程,数据结构和算法设计的更多信息。我建议Google优秀的 python系列


This is a question based on nested dictionaries.

We are given a nested dictionary wherein in the outer dictionary, the name of the match is mentioned and the value for the matches is another dictionary with it's key and values respectively and the name of the function is orangecap(d) which accepts a dictionary in the below format.

Here's the sample.

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

So I would like to search by the player key and calculate the total sum for each player and return the largest sum.

So the output needs to be ('player3', 100)

This is what i have tried so far but to no avail:

def orangecap(d):
  total=0
  for key,value in d.items():
      for value in d.items():
          if d[key] in d.keys():
              total = total+d[value]
          return(d[key],max(total))

解决方案

This is a slightly modified answer taken from a previous answer of mine.

def find_totals(d):
    total = {}
    for match, results in d.items():
        for player, score in results.items():
            total[player] = total.get(player, 0) + score
    highest_score = max(total, key=total.get)
    return highest_score, total[highest_score]

Sample output:

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


So what's going on with your code?, Let's examine the algorithm:

Firstly, you iterate through the items (keys/values) of d. This is fine, as you're trying to traverse a nested dictionary structure. However instead of traversing the inner most structure using the second for loop (iterating over value, instead of d), you instead traverse d again.

value is now simply a tuple of key/value stores in d, rather than the nested dictionary. d[key] is simply the value mapped to the match keys. So how then could a value be in the list of keys -> d.keys() Your if condition never evaluates to true. Despite this, you end up short-circuiting the entire iteration to the return statement after only two iterations. Which neither returns the correct player (d[key] is the nested dictionary), and max takes an iterable argument, not an int.

You should learn more about basic control flow, data structures, and algorithm design. I would suggest Google's excellent python series.

这篇关于查找嵌套字典值中值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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