首页 > 解决方案 > 如何在没有方括号和撇号的新行中打印出python中的字典列表

问题描述

如何打印出这样的字典列表?:

Tino Black
Alex Blue
Meow Red
Woof White

我希望左列是名称,右列是每个名称的颜色。

这是我的代码:

ask = input("Name and colour: ")
colourList = {}

while ask:
  name, colour = ask.split()
  colourList[name] = colour
  ask = input("Name and colour: ")

print(colourList)

标签: pythonpython-3.xdictionary

解决方案


您可以这样做,例如:

for name, color in colourList.items(): # this is actually a colourDictionary
    print(name, color)

推荐阅读