【问题标题】:Storing every iteration of dictionary comprehension in new nested dictionary将字典理解的每次迭代存储在新的嵌套字典中
【发布时间】:2020-05-28 02:48:07
【问题描述】:

为冗余道歉,已经尝试了 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_STD1 是字符串/播放器名称的列表。 p_stat_type 是验证结果字典中的键的字符串列表(rp_year_STD 是从电子表格编译的列表列表,每列的标题可能是结果字典中的键)。

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

我想要的是为名册和 rp_year_STD 中包含的每个球员创建一个新字典,并将其存储在相应的球队字典中,但我只是创建 1 个相同的球员字典(当然包含密钥/ rp_year_STD 中最后一个条目或电子表格中数据来源的行的值对)用于每个团队字典。

很抱歉,如果这没有意义,非常感谢此处发布的所有信息。谢谢。

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

这是我与每个列表的列标题/[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-Jax']

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

【问题讨论】:

  • (1) 球员的球队字典中的键应该是什么?选手姓名? (2) 如果“roster”是一个列表列表,那么“player”就是一个列表。
  • 也许你应该在循环之前创建列表和append() 来列出 - stats['rp'][team] = []stats['rp'][team].append( {stat_type: ...} )。或者也许你不应该使用字典压缩,而是使用普通循环和stats['rp'][team][stat_type] = ... 或者你应该使用player 作为键stats['rp'][team][player] = {stat_type: ...}
  • 在字典理解中,您不会使用 player 仅获取选定玩家的值。也许你应该使用rp_year_STD[1][player] 而不是rp_year_STD
  • 我不知道你的数据,所以我无法回答。您应该向问题添加一些数据,并且可能是最少的工作代码,然后我们可以看到问题所在,我们可以测试想法。
  • @furas 嘿,我的问题的最终解决方案实现了您的建议。谢谢。

标签: python dictionary for-loop


【解决方案1】:

我最终解决了这个问题 1. 正如@furas 建议的那样,使用播放器作为密钥 - 感谢您的宝贵时间! 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2019-10-17
    相关资源
    最近更新 更多