我无法遍历嵌套字典

时间:2019-02-25 13:58:12

标签: python

我有以下嵌套字典

  [TestMethod]
    public void EventLogTest()
    {
        MyApp myApp = new MyApp();
        Thread.Sleep(2000);
        var appStartedLog = GetEventRecordFromEventID(120);
        Assert.IsNotNull(appStartedLog);
    }

我使用以下代码将其转换为可迭代的

echo \$(hostname -f) 

现在我正尝试使用以下代码对其进行迭代

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

第一个语句返回api,但是第二个在控制台StopIteration中给出。我做错了。请帮助我

2 个答案:

答案 0 :(得分:0)

max_result_window仅具有一个密钥,即nested_dictionary

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

答案 1 :(得分:0)

您可以创建一个检查字典中每个项目的函数,如果该项目不是另一个字典,则可以打印结果。如果该项目是另一本词典,则可以对该层重复该功能。

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