这是我的代码
print(data['a'][0]['aa'])
print(data['a'][0].keys())
输入->
data={
'a':[{
'aa':{'aax':5,'aay':6,'aaz':7},
'ab':{'abx':8,'aby':9,'abz':10}
},
{
'aaa':{'aaax':11,'aaay':12,'aaaz':13},
'aab':{'aabx':14,'aaby':15,'aabz':16}
}]
}
如何打印出这样的字典
Output:
Key:aax Value: 5
Key:aay Value: 6
Key:aaz Value: 7
Key:abx Value: 8
Key:aby Value: 9
Key:abz Value: 10
Key:aaax Value: 11
如何在这种类型的数据中循环。我如何循环并打印所有数据,我可以访问单个数据,但如何打印所有数据。
使用简单的for loop
for outer_list in data['a']:
for outer_key, outer_value in outer_list.items():
for key, value in outer_value.items():
print("Key: {}, Value: {}".format(key, value))
输出:
Key: aax, Value: 5
Key: aay, Value: 6
Key: aaz, Value: 7
Key: abx, Value: 8
Key: aby, Value: 9
Key: abz, Value: 10
Key: aaax, Value: 11
Key: aaay, Value: 12
Key: aaaz, Value: 13
Key: aabx, Value: 14
Key: aaby, Value: 15
Key: aabz, Value: 16