将字典打印到表中 [英] Print a dictionary into a table
本文介绍了将字典打印到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字典:
dic={'Tim':3, 'Kate':2}
我想输出为:
Name Age
Tim 3
Kate 2
这是首先将其转换为字典列表的好方法
Is it a good way to first convert them into a list of dictionaries,
lst = [{'Name':'Tim', 'Age':3}, {'Name':'Kate', 'Age':2}]
然后通过 http://stackoverflow.com/a/10373268/156458 ?
或者有更好的方法有意义吗?
Or is there a way better in some sense?
推荐答案
嗯,你不必在字典中转换,你可以直接:
Well, you don't have to convert it in a dictionary, you can directly:
print('Name Age')
for name, age in dic.items():
print('{} {}'.format(name, age))
这篇关于将字典打印到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文