这是字典
cars = {'A':{'speed':70,
'color':2},
'B':{'speed':60,
'color':3}}
使用这个for循环
for keys,values in cars.items():
print(keys)
print(values)
它打印以下内容:
B
{'color': 3, 'speed': 60}
A
{'color': 2, 'speed': 70}
但我希望程序像这样打印它:
B
color : 3
speed : 60
A
color : 2
speed : 70
我刚开始学习字典,所以我不知道该怎么做。
最佳答案
for x in cars:
print (x)
for y in cars[x]:
print (y,':',cars[x][y])
输出:
A
color : 2
speed : 70
B
color : 3
speed : 60
关于python - 如何在 Python 中逐行打印字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785719/