字典到表格的形式

问题描述 投票:-1回答:1

我有字典

value_dict = {
  'name': 'suresh', 
  'age':23, 
  'subject': ['tamil', 'english', 'maths', 'science']
}

实际上,我需要表格形式的输出,因为我的键是我的标题,值就像,

name     age    subject
suresh   23     tamil
suresh   23     english
suresh   23     maths
suresh   23     science

提前感谢

python-2.7 dictionary
1个回答
0
投票

由于每个主题值都需要一行,因此必须迭代value_dict["subject"]

for sub in value_dict["subject"]:
    # I leave formatting details to you...
    print("{name} {age} {sub}".format(sub=sub, **value_dict)) 
© www.soinside.com 2019 - 2024. All rights reserved.