Python 2.7:打印不带括号和引号的字典 [英] Python 2.7: Print a dictionary without brackets and quotation marks

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

问题描述

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

因此,我想打印出字典.但我想这样做,就像看起来是实际的事情清单一样.我不能只做print myDict,因为它会将所有难看的东西留在里面.我希望输出看起来像Harambe : Gorilla, Restaurant : Place, etc

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

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