我不能重复使用嵌套字典

2024-04-19 09:59:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下列词典

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
          }
        }
      }
    }

我用下面的代码把它转换成iterable

dict_to_iterable = iter(nested_dictionary)

现在我试着用下面的代码编写它

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

第一个语句返回api,但第二个语句返回控制台StopIteration。我做错了什么。请帮帮我


Tags: namehttpscomapiidwwwcountrystart
2条回答

您可以创建一个函数来检查字典的每个项,如果该项不是另一个字典,则可以打印结果。如果该项是另一个字典,则可以对该层重复该函数。你知道吗

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)

这将输出:

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

nested_dictionary只有一个键,即"api"。你知道吗

如果您希望迭代其他内容,则需要修改代码。你知道吗

相关问题 更多 >