简体   繁体   English

Python从有序的字典中获取密钥

[英]Python get keys from ordered dict

python noob here. python noob在这里。 So I'm making a program that will take a JSON file from a url and parse the information and put it into a database. 因此,我正在编写一个程序,该程序将从URL中获取JSON文件并将其解析为信息并将其放入数据库中。 I have the JSON working, thankfully, but now I am stuck, I'll explain it through my code. 幸运的是,我可以使用JSON,但是现在我陷入了困境,我将通过代码进行解释。

playerDict = {
            "zenyatta" : 0,
            "mei" : 0,
            "tracer" : 0,
            "soldier76" : 0,
            "ana" : 0,
            ...}

So this is my original dictionary with the which I then fill with the players data for each hero. 这是我的原始字典,然后将其填充每个英雄的玩家数据。

topHeroes = sorted(playerDict.items(),key = operator.itemgetter(1),reverse = True)

I then sort this list and it turns the heroes with the most amount of hours played first. 然后,我对该列表进行排序,然后将播放时间最多的英雄排在第一位。

topHeroesDict = topHeroes[0:3]
playerDict['tophero'] = topHeroesDict[0]

I then get the top three heroes. 然后,我获得了前三名英雄。 The second line here prints out a list like so: 第二行在这里打印出如下列表:

'secondhero': ('mercy', 6.0)

Whereas I want the output to be: 而我希望输出为:

'secondhero': 'mercy'

Would appreciate any help i have tried the code below with and without list. 将不胜感激,我尝试过以下带有和不带有列表的代码。

list(topHeroes.keys())[0]

So thanks in advance and apologies for the amount of code! 因此,在此先感谢您的歉意,并感谢您的代码量!

You're sorting an iterable of tuples returned by the items method of the dict, so each item in the sorted list is a tuple containing the hero and their score. 您正在对由dict的items方法返回的元组进行迭代 ,因此排序列表中的每个项目都是一个包含英雄及其得分的元组。

You can avoid using sorted and dict.items altogether and get the leading heroes (without their score) by simply using collections.Counter and then getting the most_common 3 heroes. 您可以避免完全使用sorteddict.items ,而只需使用collections.Counter即可获得领先的英雄(不计其分数),然后获得most_common 3个英雄。

from collections import Counter


player_dict = Counter(playerDict)
leading_heroes = [hero for hero, _ in player_dict.most_common(3)]

You could take an approach with enumerate, if instead of "firsthero" you are ok with "Top 1" and so on. 您可以采用枚举的方法,如果您对“ Top 1”感到满意,则可以选择“ Tophero”,以此类推。 With enumerate you can iterate over the list and keep track of the current index, which is used to name the key in this dictionary comprehension. 使用枚举,您可以遍历列表并跟踪当前索引,该索引用于命名此字典理解中的键。 j[0] is the name of the hero, which is the first element of the tuple. j [0]是英雄的名称,它是元组的第一个元素。

topHeroes = sorted(playerDict.items(),key = operator.itemgetter(1),reverse = True)
topHeroesDict = {"Top "+str(i): j[0] for i, j in enumerate(topHeroes[0:3])}

Alternatively, you could use a dictionary which maps the index to first like this: 另外,您可以使用字典将索引映射到第一个字典,如下所示:

topHeroes = sorted(playerDict.items(),key = operator.itemgetter(1),reverse = True)
top = {0: "first", 1: "second", 2: "third"}
topHeroesDict = {top[i]+"hero": j[0] for i, j in enumerate(topHeroes[0:3])}

You do not need any imports to achieve this. 您不需要任何导入即可实现此目的。 Without itemgetter, you can do it in one line like this: 没有itemgetter,您可以像这样在一行中完成:

top = {0: "first", 1: "second", 2: "third"}
topHeroesDict = {top[i]+"hero": j[0] for i, j in enumerate(sorted([(i, playerDict[i]) for i in playerDict.keys()], key = lambda x: x[1], reverse = True)[0:3])}

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

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