打印没有括号和引号的字典

打印没有括号和引号的字典

本文介绍了Python 2.7:打印没有括号和引号的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

So, I want to print out a dictionary. But I want to do it like it looks like an actual list of things. I can't just do print myDict, as it will leave all the ugly stuff in. I want the output to look like Harambe : Gorilla, Restaurant : Place, etc

那我该怎么办?我还没有找到我想要的帖子会议.提前致谢.

So what do I do? I haven't found a post meeting what I want. Thanks in advance.

推荐答案

使用items字典方法:

print('
'.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:打印没有括号和引号的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:19