我有下列词典
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。我做错了什么。请帮帮我
您可以创建一个函数来检查字典的每个项,如果该项不是另一个字典,则可以打印结果。如果该项是另一个字典,则可以对该层重复该函数。你知道吗
这将输出:
nested_dictionary
只有一个键,即"api"
。你知道吗如果您希望迭代其他内容,则需要修改代码。你知道吗
相关问题 更多 >
编程相关推荐