添加一个包含json响应中的多个值的字典键的列表



我正在遍历一个json响应,其中统计数据按所玩的游戏分组。我想从集合中收集所有的值,并为它们分配一个字典键。目前,每个值都有自己的键。下面是json响应的一个示例:

{'data': [{'ast': 9,
'blk': 0,
'dreb': 4,
'fg3_pct': 0.0,
'fg3a': 2,
'fg3m': 0,
'fg_pct': 0.6,
'fga': 20,
'fgm': 12,
'ft_pct': 0.333,
'fta': 3,
'ftm': 1,
'game': {'date': '2003-10-29T00:00:00.000Z',
'home_team_id': 26,
'home_team_score': 106,
'id': 15946,
'period': 4,
'postseason': False,
'season': 2003,
'status': 'Final',
'time': ' ',
'visitor_team_id': 6,
'visitor_team_score': 92},
'id': 361330,
'min': '42:50',
'oreb': 2,
'pf': 3,
'player': {'first_name': 'LeBron',
'height_feet': 6,
'height_inches': 8,
'id': 237,
'last_name': 'James',
'position': 'F',
'team_id': 14,
'weight_pounds': 250},
'pts': 25,
'reb': 6,
'stl': 4,
'team': {'abbreviation': 'CLE',
'city': 'Cleveland',
'conference': 'East',
'division': 'Central',
'full_name': 'Cleveland Cavaliers',
'id': 6,
'name': 'Cavaliers'},
'turnover': 2},
{'ast': 8,
'blk': 0,
'dreb': 10,
'fg3_pct': 0.2,
'fg3a': 5,
'fg3m': 1,
'fg_pct': 0.471,
'fga': 17,
'fgm': 8,
'ft_pct': 0.571,
'fta': 7,
'ftm': 4,
'game': {'date': '2003-10-30T00:00:00.000Z',
'home_team_id': 24,
'home_team_score': 95,
'id': 16277,
'period': 4,
'postseason': False,
'season': 2003,
'status': 'Final',
'time': ' ',
'visitor_team_id': 6,
'visitor_team_score': 86},
'id': 361523,
'min': '40:21',
'oreb': 2,
'pf': 1,
'player': {'first_name': 'LeBron',
'height_feet': 6,
'height_inches': 8,
'id': 237,
'last_name': 'James',
'position': 'F',
'team_id': 14,
'weight_pounds': 250},
'pts': 21,
'reb': 12,
'stl': 1,
'team': {'abbreviation': 'CLE',
'city': 'Cleveland',
'conference': 'East',
'division': 'Central',
'full_name': 'Cleveland Cavaliers',
'id': 6,
'name': 'Cavaliers'},
'turnover': 7},

这是我的代码:


players_stats=[]
players_game_stats_url="https://balldontlie.io/api/v1/stats?season[]=2018&player_ids[]=237"
result=requests.get(players_game_stats_url).json()
for assists in result['data']:
players_stats.append({"Assists per game":assists['ast']})

我想生产的是这样的东西:

{场均助攻:9,8,6,7,3}

但是我得到的是:

[{'Assists per game': 9},
{'Assists per game': 8},
{'Assists per game': 6},
{'Assists per game': 7},
{'Assists per game': 3},
{'Assists per game': 9},
{'Assists per game': 4},
{'Assists per game': 7},
{'Assists per game': 3},
{'Assists per game': 8},
{'Assists per game': 8},
{'Assists per game': 8},
{'Assists per game': 2},
{'Assists per game': 4},
{'Assists per game': 9},
{'Assists per game': 7},
{'Assists per game': 7},
{'Assists per game': 5},
{'Assists per game': 8},
{'Assists per game': 5},
{'Assists per game': 3},
{'Assists per game': 9},
{'Assists per game': 4},
{'Assists per game': 6},
{'Assists per game': 3}]

基于Kenny Ostrom的评论

output = {"Assists per game": [game_info['ast'] for game_info in result['data']]}

它的结果就像你问的

{'Assists per game': [9, 8]}

相关内容

  • 没有找到相关文章

最新更新