简体   繁体   English

我无法遍历嵌套字典

[英]I can not to iterate through nested dictionary

I have following nested dictionary 我有以下嵌套字典

nested_dictionary = {
      "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
          }
        }
      }
    }

i convert it to iterable with following code 我用以下代码将其转换为可迭代

dict_to_iterable = iter(nested_dictionary)

Now i am trying to iter it with following code 现在,我尝试使用以下代码对其进行迭代

print(next(dict_to_iterable))
print(next(dict_to_iterable)) 

First statement return api but second give in console StopIteration. 第一条语句返回api,但第二条返回控制台StopIteration。 What i am doing wrong. 我做错了。 Please help me 请帮我

nested_dictionary is having only one key ie "api" . nested_dictionary仅具有一个键,即"api"

If you were expecting to iterate over something else, you need to modify your code. 如果希望迭代其他内容,则需要修改代码。

You can create a function that check each item of a dictionary and if that item is not another dictionary then it can print the result. 您可以创建一个检查字典中每个项目的函数,如果该项目不是另一个字典,则可以打印结果。 If the item is another dictionary then you can repeat the function for this layer. 如果该项目是另一个词典,则可以对该层重复该功能。

def iterate_dictionary(d):
    for key in d.keys():
        # check if each item is dictionary
        if str(type(d[key])) == "<class 'dict'>":
            iterate_dictionary(d[key])
        else:
            print (key, d[key])

nested_dictionary = {
      "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
          }
        }
      }
    }


iterate_dictionary(nested_dictionary)

This outputs: 输出:

results 4
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
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
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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM