标记为重复的示例不涉及嵌套字典。尽管我在duplicate
that uses中尝试了示例,collections.OrderedDict()
但它要么给出list index out of range
要么根本不打印任何东西。
数据与此类似:
[ {
"name":"frog",
"age":33,
"color":"green",
"weight":86,
"can_jump": true,
--- other data and so on ---
},
{
-- other data too ---
}
]
这是我用来检索前四个数据并将其存储在结果列表中的代码。
i = 0
while i < len(data_file):
key = ["name", "age", "color", "weight"]
results = []
for x in data_file[i]:
results.append(data_file[i][x])
j = 0
while j < 4:
print("{}: {}".format(key[j], results[j]))
j+=1
print()
i+=1
但是当我打印出决赛时,
结果它们随机显示,例如
name: 33
age:86
color: true
weight: green