如何解决python中的键错误?检查下面的代码

2024-10-02 02:30:26 发布

您现在位置:Python中文网/ 问答频道 /正文

def myFUNC (e):
  return e["Points"]

team_stats = [
            {"76ers" : {"Wins" : 22,
                       "Losses" : 12,
                       "Points" : 647},
            "Nets" :  {"Wins" : 22,
                       "Losses" : 13,
                       "Points" : 629},
            "Bucks" :  {"Wins" : 21,
                       "Losses" : 13,
                       "Points" : 618}}
              ]
print(team_stats.sort(key=myFUNC))
KeyError: 'Points'

Tags: keyreturndefstatsmyfuncsortteampoints
3条回答

获取KeyError的原因是,您试图从字典的外部级别访问键"Points",该键不属于该级别,即使它属于当前结构,您也不会看到结果上的差异

在一个list中有一个dict,没有什么要sort。如果要对dict进行排序,则只有在使用3.7+时才可能,因为从3.7开始,^{}s in Python are guaranteed to maintain order。如果确实满足版本要求,则使用:

def myFUNC (e):
  return e[1]["Points"]

team_stats = [
            {"76ers" : {"Wins" : 22,
                       "Losses" : 12,
                       "Points" : 647},
            "Nets" :  {"Wins" : 22,
                       "Losses" : 13,
                       "Points" : 629},
            "Bucks" :  {"Wins" : 21,
                       "Losses" : 13,
                       "Points" : 618}}
              ]
result = [dict(sorted(team_stats[0].items(), key=myFUNC))]
print(result)

[{'Bucks': {'Wins': 21, 'Losses': 13, 'Points': 618},
  'Nets': {'Wins': 22, 'Losses': 13, 'Points': 629},
  '76ers': {'Wins': 22, 'Losses': 12, 'Points': 647}}]

如果不满足版本要求,则需要使用^{}

from collections import OrderedDict
# replace the `result` line with this:
result = [OrderedDict(sorted(team_stats[0].items(), key=myFUNC))]

其中:

[OrderedDict([('Bucks', {'Wins': 21, 'Losses': 13, 'Points': 618}),
              ('Nets', {'Wins': 22, 'Losses': 13, 'Points': 629}),
              ('76ers', {'Wins': 22, 'Losses': 12, 'Points': 647})])]

但我不明白你为什么需要这个清单

另外,list().sort()修改list的位置,这不仅在这里是无用的,打印它将返回None

我想你的意思是这可能

def myFUNC (i):
  return i[1]["Points"]

team_stats =  {"76ers" : {"Wins" : 22,
                       "Losses" : 12,
                       "Points" : 647},
            "Nets" :  {"Wins" : 22,
                       "Losses" : 13,
                       "Points" : 629},
            "Bucks" :  {"Wins" : 21,
                       "Losses" : 13,
                       "Points" : 618}}

print(sorted(team_stats.items(), key=myFUNC))

您可以按点对每个名称进行排序

问题是列表中有一个字典,所以当您尝试对列表进行排序时,它会查看外部字典,而不是内部字典

解决方案是只使用字典项,如下所示:

team_stats =     {"76ers" : {"Wins" : 22,
                       "Losses" : 12,
                       "Points" : 647},
            "Nets" :  {"Wins" : 22,
                       "Losses" : 13,
                       "Points" : 629},
            "Bucks" :  {"Wins" : 21,
                       "Losses" : 13,
                       "Points" : 618}}
sorted_teams = list(team_stats.items())  
sorted_teams.sort(key = lambda team : team[1]["Points"])   
# [('Bucks', {'Wins': 21, 'Losses': 13, 'Points': 618}), ('Nets', {'Wins': 22, 'Losses': 13, 'Points': 629}), ('76ers', {'Wins': 22, 'Losses': 12, 'Points': 647})]

相关问题 更多 >

    热门问题