简体   繁体   English

比较由字典 Python 制成的列表的字典

[英]Comparing dicts of lists made from dicts Python

Sorry for the confusion this problem can cause you.很抱歉这个问题可能给您带来的困惑。

I have a dict which keys are country names and values that are lists of dicts, see example below:我有一个字典,其中键是国家名称,值是字典列表,请参见下面的示例:

{'spain': [{'gold': 3}, {'silver': 1}, {'bronze': 0}], 'colombia': [{'gold': 2}, {'silver': 0}, {'bronze': 0}]}

I need to compare them and get the one that has more gold medals, but I don't know any way of doing it.我需要比较它们并获得金牌更多的那个,但我不知道有什么方法可以做到这一点。

PS: I need to return the country with the most medals in the same way as above: PS:我需要用和上面一样的方式返回奖牌最多的国家:

{'country':[{'gold':3}, {'silver':3}, {'bronze':3}]}

Edit: Clarification编辑:澄清

You could use max with a custom key function:您可以将max与自定义key function 一起使用:

dict([max(d.items(), key=lambda x: x[1][0]['gold'])])
# {'spain': [{'gold': 3}, {'silver': 1}, {'bronze': 0}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM