gpt4 book ai didi

python - 我需要创建一个生成嵌套字典的 for 循环

转载 作者:行者123 更新时间:2023-11-28 16:59:28 25 4
gpt4 key购买 nike

我需要创建一个 for 循环,每次检测到一个不存在的键时生成一个新的嵌套字典。我正在从之前的函数中获取外部字典的信息。

  • 它需要创建一个字典,以可用的运 Action 为它的键,一个字典作为它的值(value)。
  • 在内部字典中,运动员姓名将用作其关键字和奖牌数(整数)将是它的值(value)。键 = 运动,值 = {: }CSE 231 2019 年 Spring
  • 该函数将遍历 get_country_stats() 中的字典以查找运动员、运动和奖牌。请注意,当您想为一项新运动添加运动员时,您需要先为该运动创建一个空字典,然后再向其中添加运动员。
  • 奖牌类型(金牌、银牌、铜牌)与我们的新字典无关,它们都是视为1枚勋章。

我启动了两个空字典,外字典和内字典。然后创建一个外部 for 循环遍历所有键值对并返回一个列表

def display_best_athletes_per_sport(Athlete, Country, Sports):
medals = 0
outer_dict = {}
inner_dict = {}
for key, value in Country.items():
for item in value:
athlete = item[0]
medals = item[5]
sport = item[3]
inner_dict = {athlete:medals}
outer_dict = {sport:inner_dict}
if sport not in outer_dict:
new_dict[sport] = value[i]
if medals in value:
medals += 1
else:
medals = 1

如果找不到所需的键(运动),我希望能够生成一个新的外部字典,然后在每次为特定运动员找到奖牌时更新内部字典的值。

这是我现在尝试创建的函数中使用的 Country 函数的输出:

{'FIN': [
('Juhamatti Tapio Aaltonen', 'Finland', 2014, 'ice hockey', "ice hockey men's ice hockey", 'bronze'),
('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's individual all-around", 'bronze'),
('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's team all-around", 'gold'),
('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's horse vault", 'gold'),
('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's pommelled horse", 'gold'),
('Paavo Johannes Aaltonen', 'Finland', 1952, 'gymnastics', "gymnastics men's team all-around", 'bronze')],
'NOR': [
('Kjetil Andr Aamodt', 'Norway', 1992, 'alpine skiing', "alpine skiing men's super g", 'gold'),
('Kjetil Andr Aamodt', 'Norway', 1992, 'alpine skiing', "alpine skiing men's giant slalom", 'bronze'),
('Kjetil Andr Aamodt', 'Norway', 1994, 'alpine skiing', "alpine skiing men's downhill", 'silver'),
('Kjetil Andr Aamodt', 'Norway', 1994, 'alpine skiing', "alpine skiing men's super g", 'bronze'),
('Kjetil Andr Aamodt', 'Norway', 1994, 'alpine skiing', "alpine skiing men's combined", 'silver'),
('Kjetil Andr Aamodt', 'Norway', 2002, 'alpine skiing', "alpine skiing men's super g", 'gold'),
('Kjetil Andr Aamodt', 'Norway', 2002, 'alpine skiing', "alpine skiing men's combined", 'gold'),
('Kjetil Andr Aamodt', 'Norway', 2006, 'alpine skiing', "alpine skiing men's super g", 'gold'),
('Ann Kristin Aarnes', 'Norway', 1996, 'football', "football women's football", 'bronze')],
'NED': [('Pepijn Aardewijn', 'Netherlands', 1996, 'rowing', "rowing men's lightweight double sculls", 'silver')]}

最佳答案

好吧,猫已经从袋子里出来了。所以这是我对此的看法。我认为最好将检查条目是否出现在字典中与向该条目添加内容分开。因此,当您添加条目时,它始终“还没有任何内容”。这使您可以以相同的方式处理“将下一项添加到条目”,而不管该条目是否存在。鉴于此,在我看来,这是您想要做的事情的基本想法:

def display_best_athletes_per_sport(Athlete, Country, Sports):
outer_dict = {}
for key, value in Country.items():
for item in value:
athlete = item[0]
medals = item[5]
sport = item[3]
if sport not in outer_dict:
outer_dict[sport] = {}
if athlete not in outer_dict[sport]:
outer_dict[sport][athlete] = 0
outer_dict[sport][athlete] += 1
pprint(outer_dict)

结果如下:

{'alpine skiing': {'Kjetil Andr Aamodt': 8},
'football': {'Ann Kristin Aarnes': 1},
'gymnastics': {'Paavo Johannes Aaltonen': 5},
'ice hockey': {'Juhamatti Tapio Aaltonen': 1},
'rowing': {'Pepijn Aardewijn': 1}}

这与@gmds 提供的答案相同,因此两者都是解决问题的有效方法,并且非常相似。

关于python - 我需要创建一个生成嵌套字典的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com