Python 如何获取dict列表的索引并将dict添加到每个索引中?

Python 如何获取dict列表的索引并将dict添加到每个索引中?,python,loops,dictionary,Python,Loops,Dictionary,我有一个json文件,其结构如下: { "Premier League": { "abbreviation": "EN_PR", "id": 1.0, "seasons": [ { "label": "2019/20", "id": 274.0 }, { "label": "

我有一个json文件,其结构如下:

{
    "Premier League": {
        "abbreviation": "EN_PR",
        "id": 1.0,
        "seasons": [
            {
                "label": "2019/20",
                "id": 274.0
            },
            {
                "label": "2018/19",
                "id": 210.0
            }
        ]
    },
    "UEFA Champions League": {
        "abbreviation": "EU_CL",
        "id": 2.0,
        "seasons": [
            {
                "label": "Champions League Season 2019/2020",
                "id": 288.0
            },
            {
                "label": "2018/19",
                "id": 214.0
            },
        ]
}
我试图做的是,在迭代过程中,索引每个
季节id
,然后通过向其他数据源发出请求来获取有关
季节id
的信息,最后将获取的信息添加到每个
季节id
。所以它是这样的,并且在每个季节中对每个
seasure\u id
都这样做。下面是一个示例,帮助您说明我在
季节
中为每个
季节id
编制索引的意思

{
        "Premier League": {
            "abbreviation": "EN_PR",
            "id": 1.0,
            "seasons": [
                {
                    "label": "2019/20",
                    "id": 274.0,
                    "teams" :{
                         "Arsenal":{
                             "Players":{}
                                   },

                              }
                },
问题是:如何迭代每个父项的季节列表,并向每个季节id添加信息

下面打印每个季度id,但我不确定如何访问它们并向它们添加信息

 for key in parent_key:
    seasons = file[key]['seasons']
    for season in seasons:
        print(season['id'])

如果可能,您可能会发现,如果您经常按id更新项目,那么将数据解析为以下格式就容易得多:

leagues = { 
    1: {
        "name": "Premier_League",
        "seasons": { 
            274: {
                "label": "2019/20"
            }
        }
    }
}
然后,给定一个
联赛id
和一个
赛季id
,您可以轻松访问与之关联的赛季,即
联赛[league\u id][“seasons”][seasons\u id]
。您还可以通过以下方式更新该季节的信息:

league_id = 1
season_id = 274
leagues[league_id]["seasons"][season_id]["teams"] = { "Arsenal": { "players": {}}}
print(leagues)

# {1: {'name': 'Premier_League', 'seasons': {274: {'label': '2019/20', 'teams': {'Arsenal': {'players': {}}}}}}}

一般来说,
dicts
在您知道要查找的密钥时有利于快速简单的查找。

您是否意识到输入JSON中既不存在
团队
,也不存在
兵工厂
?我们如何展示如何转换JSON,使其神奇地出现在结果中?很抱歉,我的问题不清楚,我将重新定义它!谢谢这不是一个很好的数据结构。你和dict of dict of dict design有关系吗?@Scott我完全没有关系,但我的问题并不能解释我为什么使用这种方法。我能解释的最简单的方式是,我需要能够创建一个有意义的文件夹结构,图片“联盟”-“赛季”-“球队”-球员”。同时,可以让所有ID发出请求,而无需查看每个文件夹。但我很高兴听到建议,因为我对数据结构了解不多!谢谢大家!@Scott All数据也是从一个API中提取的,该API以json格式构造了is数据,因此使用dictsThank进行输入非常方便!现在我正在尝试构建一个类,该类将league_id作为参数,并更新整个league!当我试图解决一件事时,我要么半途而废,要么撞到墙上。。谢谢你花时间写下来!