首页 > 解决方案 > 将字典理解的每次迭代存储在新的嵌套字典中

问题描述

为冗余道歉,已经尝试了 3 天。

我不知道如何在嵌套字典中存储新字典。

for team, roster in team_roster.items():  
 stats['rp'][team] = {}
 for player in roster:
    if player in rp_year_STD[1]:
        stats['rp'][team] = {stat_type: stat for stat_type in p_stat_type for stat_list in rp_year_STD\
for stat in stat_list if stat_list[0] == stat_type}

试过这个,完全相同(每个团队1个重复的字典):

for team, roster in team_roster.items():  
 stats['rp'][team] = {}
 for player in roster:
     if player in rp_year_STD[1]:
         for stat_type in p_stat_type:
             for stat_list in rp_year_STD:
                  if stat_list[0] == stat_type:
                     for stat in stat_list:
                       stats['rp'][team][stat_type] = stat

Team 是单独字典中的字符串键(team_roster,如下所示) roster 是每个团队的字符串/玩家名称列表。Player 是字符串列表(名册)中的字符串。rp_year_STD 是一个列表列表。rp_year_STD 1是字符串/玩家名称的列表。p_stat_type 是验证结果字典中的键的字符串列表(rp_year_STD 是从电子表格编译的列表列表,每列的标题可能是结果字典中的键)。

我知道我正在用这个字典理解创建 1 个字典。

我想要的是为 roster 和 rp_year_STD 中包含的每个玩家创建一个新字典,并将其存储在相应的团队字典中,但我只是创建了 1 个相同的玩家字典(当然包含来自的键/值对每个团队字典的 rp_year_STD 或电子表格中的最后一个条目或数据来自的行)。

对不起,如果这没有意义,真的很感谢这里发布的所有信息。谢谢你。

这就是 rp_year_STD 是什么,每一列是一个列表中的一个列表,而 rp_year_STD 1是一个字符串/玩家名称的列表。

这是 rp_year_STD 是什么,每一列是一个带有列表的列表,[1] 是每个玩家的名字,可能会或可能不会包含在玩家列表中

这是我与每个列表的列标题/[0] 索引进行比较的键列表。我不希望每个列/列表项/值都在结果字典中,这就是我使用列表 p_stat_type 验证的原因(如下所示):

p_stat_type = ['Name', 'TBF', 'H', '2B', '3B', 'R', 'ER', 'HR', 'BB', 'IBB', 'HBP', 'SO', 'AVG', 'OBP', 'SLG', 'wOBA', 'playerId',  'K/BB', 'HR/9',\
           'K%', 'BB%', 'K-BB%', 'AVG', 'WHIP', 'BABIP', 'LOB%', 'FIP', 'xFIP', 'playerId' 'GB/FB', 'LD%', 'GB%', 'FB%', 'IFFB%', 'HR/FB', 'IFH%', 'BUH%', \
               'Pull%', 'Cent%', 'Oppo%', 'Soft%', 'Med%', 'Hard%', 'playerId']

这是放在每个 [team] 键中的重复字典:

'CHC': {'Name': 'Emmanuel Clase', 'TBF': '89', 'H': '19', '2B': '5', '3B': '1', 'R': '8', 'ER': '6', 'HR': '2', 'BB': '4', 'IBB': '0', 'HBP': '1', 'SO': '21', 'AVG': '0.226190476', 'OBP': '0.269662921', 'SLG': '0.38095238', 'wOBA': '0.2757020506', 'playerId': '21032'}

非常接近工作,但这只是 rp_year_STD 中每个列表的最后一个值。

这是 team_roster 字典,函数 team_roster 返回每个团队的字符串\当前球员列表:

team_roster = {'ARI' : team_roster(ARI, 'Diamondbacks'),
'BAL' : team_roster(BAL, 'Orioles'),
'BOS' : team_roster(BOS, 'Red Sox'),
'CHC' : team_roster(CHC, 'Cubs'),
'CIN' : team_roster(CIN, 'Reds'),
'CLE' : team_roster(CLE, 'Indians'),
'COL' : team_roster(COL, 'Rockies'),
'DET' : team_roster(DET, 'Tigers'),
'HOU' : team_roster(HOU, 'Astros'),
'KC' : team_roster(KC, 'Royals'),
'LAD' : team_roster(LAD, 'Dodgers'),
'WSH' : team_roster(WSH, 'Nationals'),
'NYM' : team_roster(NYM, 'Mets'),
'OAK' : team_roster(OAK, 'Athletics'),
'PIT' : team_roster(PIT, 'Pirates'),
'SD' : team_roster(SD, 'Padres'),
'SEA' : team_roster(SEA, 'Mariners'),
'SF' : team_roster(SF, 'Giants'),
'STL' : team_roster(STL, 'Cardinals'),
'TB' : team_roster(TB, 'Rays'),
'TEX' : team_roster(TEX, 'Rangers'),
'TOR' : team_roster(TOR, 'Blue Jays'),
'MIN' : team_roster(MIN, 'Twins'),
'PHI' : team_roster(PHI, 'Phillies'),
'ATL' : team_roster(ATL, 'Braves'),
'CWS' : team_roster(CWS, 'White Sox'),
'MIA' : team_roster(MIA, 'Marlins'),
'NYY' : team_roster(NYY, 'Yankees'),
'MIL' : team_roster(MIL, 'Brewers')} 

team_roster(teamA, 'TeamA') 将是 ['A. 男人','A-贾克斯']

再次感谢大家的任何建议。

标签: pythondictionaryfor-loop

解决方案


正如@furas 建议的那样,我最终通过 1. 使用播放器作为密钥解决了这个问题 - 感谢您的宝贵时间!2. 在字典理解中引用玩家的索引,并从理解中删除嵌套循环,并再次按照@furas 的建议添加正常循环。所以,希望这有一些用处和/或我收到一些明智的改进建议。谢谢,下面是我最终得到的代码:

  for team in team_roster.keys(): 
  stats['hitter'][team] = {}
  for player in sort_roster(team)[0]:
      if player in h_zips[0]:         
          h_zips_index = h_zips[0].index(player)
          index = h_year_STD[1].index(player)
          if h_zips[1][h_zips_index] == team and h_zips[25][h_zips_index] == h_year_STD[23][index]:
              stats['hitter'][team][player] = {}
              for stat_list in h_year_STD:
                  player_dict = {stat_type: stat_list[index] for stat_type in h_stat_type if stat_list[0] == stat_type}
                  stats['hitter'][team][player].update(player_dict)

推荐阅读