如何遍历嵌套的Python字典

时间:2019-02-23 10:08:51

标签: python dictionary

我从API中提取了json数据,并将其转换为python字典

response =    {
      "api": {
        "results": 4,
        "leagues": {
          "22": {
            "league_id": "22",
            "name": "Ligue 1",
            "country": "France",
            "season": "2017",
            "season_start": "2017-08-04",
            "season_end": "2018-05-19",
            "logo": "https://www.api-football.com/public/leagues/22.svg",
            "standings": true
          },
          "24": {
            "league_id": "24",
            "name": "Ligue 2",
            "country": "France",
            "season": "2017",
            "season_start": "2017-07-28",
            "season_end": "2018-05-11",
            "logo": "https://www.api-football.com/public/leagues/24.png",
            "standings": true
          },
          "157": {
            "league_id": "157",
            "name": "National",
            "country": "France",
            "season": "2017",
            "season_start": "2017-08-04",
            "season_end": "2018-05-11",
            "logo": "https://www.api-football.com/public/leagues/157.png",
            "standings": true
          },
          "206": {
            "league_id": "206",
            "name": "Feminine Division 1",
            "country": "France",
            "season": "2017",
            "season_start": "2017-09-03",
            "season_end": "2018-05-27",
            "logo": "https://www.api-football.com/public/leagues/206.png",
            "standings": true
          }
        }
      }
    }

现在我要遍历此嵌套词典,我需要提取此嵌套词典中的所有第三词典。所需数据的键是“ 22”,“ 24”,“ 157”,“ 206”,以便更好地理解所需的字典是

"22": {
                "league_id": "22",
                "name": "Ligue 1",
                "country": "France",
                "season": "2017",
                "season_start": "2017-08-04",
                "season_end": "2018-05-19",
                "logo": "https://www.api-football.com/public/leagues/22.svg",
                "standings": true
              }

我正在尝试通过此代码对其进行迭代

for i in response["api"]["leagues"]["22"] 

但是我的问题是API可以返回任何数量的结果,而我不知道所需数据的键。如果我不知道所需数据的键,我该如何遍历它

1 个答案:

答案 0 :(得分:0)

最佳方法可能取决于您在遍历条目时对数据的处理方式,但是:

response_leagues = response['api']['leagues']

仅限于您​​关心的内容。然后注意

[league for league in response_leagues]

在此词典中提供的列表(在您的示例中为['22', '24', '157', '206'])。

所以循环遍历这些最低级别字典中的每个条目的一种方法是:

for league in response_leagues:
for x in response_leagues[league]:
    #do something with x (the key)
    # and response_leagues[league][x] (the value)
    # for example:
    print(league, x, response_leagues[league][x])

输出:

22 league_id 22  
22 name Ligue 1  
22 country France  
22 season 2017  
22 season_start 2017-08-04  
22 season_end 2018-05-19  
22 logo https://www.api-football.com/public/leagues/22.svg  
22 standings True 24 league_id 24  
24 name Ligue 2 
...

或者,您也可以通过遍历键值对(项)来获得相同的结果:

for league in response_leagues:
for x in response_leagues[league].items():
    #here x[0] is the key and x[1] is the value
    print(league, x[0], x[1])