python 2.7 : Print a dictionary without brackets and quotation marks

标签 python python-2.7 dictionary printing

myDict = {"Harambe" : "Gorilla", "Restaurant" : "Place", "Codeacademy" : "Place to learn"}

所以,我想打印一本字典。但我想做的就像它看起来像一个实际的事情 list 。我不能只做 print myDict,因为它会留下所有丑陋的东西。我希望输出看起来像 Harambe : Gorilla, Restaurant : Place, etc

那我该怎么办?我还没有找到我想要的 session 后。提前致谢。

最佳答案

使用items字典方法:

print('\n'.join("{}: {}".format(k, v) for k, v in myDict.items()))

输出:

Restaurant: Place
Codeacademy: Place to learn
Harambe: Gorilla

展开:

for key, value in myDict.items():
    print("{}: {}".format(key, value))

关于 python 2.7 : Print a dictionary without brackets and quotation marks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071006/

相关文章:

python - 升级到 Python 3 时是否应该删除 __future__ 导入和 unicode 字符串?

linux - Python 的子进程 popen 中的死锁

python-2.7 - 如何设置 IPython 笔记本 HTML 输出的 <title> 标签?

python - 通过每个类的属性创建类名字典

Python 3.6+ 格式化解包字典中缺少键的字符串

json - 快速解析 JSON 数据中的嵌套数组

python - 创建一个固定大小的列表或数组?

javascript - 在 MAMP 中使用 Javascript 调用 Python 函数

python - 无法淡出文本 - PyGame

python - 寻找集合中最大元素的错误