将词典格式化为列

2024-10-04 11:24:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在设法把这本词典的关键字与年份相符。我尝试了多种格式化方法,但都没有成功。在

我试过了:

def displayDegrees(degDict):
  print('Master degrees conferred in  fields. \n')
  print("Field of Study                    1981            2010")
  for k, v in degDict.items(): 
     print("{}       {}".format(k,v))

任何提示或提示将不胜感激。在


Tags: 方法inmasterfieldfieldsdef关键字print
2条回答
dic = {"a":"b", "aaaaaaa":"bb"}

for key in dic:
    print ("{0}\t\t{1}".format(key, dic[key]))

给出输出

enter image description here

你的字典是什么样子的还不清楚。但你可以采用下面的方法。在

d = {'Business': [100000, 200000],
     'English': [50000, 150000],
     'Mathematics': [150000, 250000]}

cols = ['Field of Study', 1981, 2010]

print("{:>20} {:>10} {:>10}".format(*cols))

for k, v in d.items():
    print("{:>20} {:>10} {:>10}".format(k, *v))

结果:

^{pr2}$

相关问题 更多 >