python - 如何在 Python 中逐行打印字典?

标签 python printing dictionary

这是字典

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/

相关文章:

python - Networx:查找节点的最大派系长度

c# - PrintDocument N of N 页

Python -> 告诉一个带有 print 语句的程序到 "not print"

c++ - 是否有打印可选值的约定?

python - 加入字符串。生成器或列表理解?

python - 使用 argparse 传递一个目录(不需要类型检查)

python - 当我在盒子上按下 onclick() 时出现 Selenium 问题

c++ - 在不破坏对象的情况下创建对象映射

c++ - 如何在 C++ 中创建 vector 的 vector 映射?

python - 使用列表理解将字符串转换为字典