Python从有序dict获取密钥
这里没有。因此,我正在制作一个程序,从url获取一个JSON文件,解析信息并将其放入数据库。谢天谢地,我已经让JSON工作了,但是现在我被卡住了,我将通过我的代码来解释它Python从有序dict获取密钥,python,python-3.x,dictionary,key,Python,Python 3.x,Dictionary,Key,这里没有。因此,我正在制作一个程序,从url获取一个JSON文件,解析信息并将其放入数据库。谢天谢地,我已经让JSON工作了,但是现在我被卡住了,我将通过我的代码来解释它 playerDict = { "zenyatta" : 0, "mei" : 0, "tracer" : 0, "soldier76" : 0, "ana" : 0, ...} 这是
playerDict = {
"zenyatta" : 0,
"mei" : 0,
"tracer" : 0,
"soldier76" : 0,
"ana" : 0,
...}
这是我的原始字典,我用它来填充每个英雄的玩家数据
topHeroes = sorted(playerDict.items(),key = operator.itemgetter(1),reverse = True)
然后我对这个列表进行排序,它会首先显示最长播放时间的英雄
topHeroesDict = topHeroes[0:3]
playerDict['tophero'] = topHeroesDict[0]
然后我得到了前三名英雄。这里的第二行打印出如下列表:
'secondhero': ('mercy', 6.0)
鉴于我希望输出为:
'secondhero': 'mercy'
如果有任何帮助,我将不胜感激。我尝试了下面的代码,有列表和没有列表
list(topHeroes.keys())[0]
因此,提前感谢并为代码量道歉 您正在对dict的items
方法返回的一组元组进行排序,因此排序列表中的每个项都是一个包含英雄及其分数的元组
您可以避免同时使用排序的和dict.items
,只需使用最常见的3个英雄,即可获得主要英雄(无得分)
from collections import Counter
player_dict = Counter(playerDict)
leading_heroes = [hero for hero, _ in player_dict.most_common(3)]
您正在对dict的items
方法返回的一组元组进行排序,因此排序列表中的每个项都是一个包含英雄及其分数的元组
您可以避免同时使用排序的和dict.items
,只需使用最常见的3个英雄,即可获得主要英雄(无得分)
from collections import Counter
player_dict = Counter(playerDict)
leading_heroes = [hero for hero, _ in player_dict.most_common(3)]
您可以使用enumerate,如果您不使用“firsthero”,而是使用“Top1”等等。使用enumerate,您可以迭代列表并跟踪当前索引,该索引用于命名此字典中的键。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])}
或者,您可以使用字典将索引映射到第一个,如下所示:
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])}
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])}
您不需要任何导入来实现这一点。如果没有itemgetter,您可以在一行中完成它,如下所示:
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])}
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])}
您可以使用enumerate,如果您不使用“firsthero”,而是使用“Top1”等等。使用enumerate,您可以迭代列表并跟踪当前索引,该索引用于命名此字典中的键。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])}
或者,您可以使用字典将索引映射到第一个,如下所示:
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])}
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])}
您不需要任何导入来实现这一点。如果没有itemgetter,您可以在一行中完成它,如下所示:
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])}
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])}
这个柜台真的很有用!谢谢你!这个柜台真的很有用!谢谢你!