如何在Python中逐行打印字典? [英] How to print a dictionary line by line in Python?

查看:2803
本文介绍了如何在Python中逐行打印字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是字典

cars = {'A':{'speed':70,
        'color':2},
        'B':{'speed':60,
        'color':3}}

使用循环

for keys,values in cars.items():
    print(keys)
    print(values)

它打印如下:

B
{'color': 3, 'speed': 60}
A
{'color': 2, 'speed': 70}

但我希望程序打印如下:

But I want the program to print it like this:

B
color : 3
speed : 60
A
color : 2
speed : 70

我刚刚开始学习字典,所以我不知道该怎么做。

I just started learning dictionaries so I'm not sure how to do this.

推荐答案

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中逐行打印字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆