如何打印从包含列表的词典中提取信息的句子

2024-09-27 02:17:53 发布

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

我有下面的字典。如果我想打印如下输出,我应该如何用python编写它

John is 20 years old with GPA 3.3.   
Shannon is 21 years old with GPA 3.4.
Eileen is 20 years old with GPA 3.5.
students = {
    101: ["John", 20, 3.3],
    102: ["Shannon", 21, 3.4],
    103: ["Eileen", 20, 3.5]
}

Tags: 字典iswithjohnoldgpashannonyears
2条回答

你可以试试这个

for data in students.values():
    print(data[0], "is ", data[1], "years old with GPA ", data[2])

这是可行的解决方案

students = {
    101: ["John", 20, 3.3],
    102: ["Shannon", 21, 3.4],
    103: ["Eileen", 20, 3.5]
}
for i in students.values():
    print(i[0]+" "+str(i[1])+" years old with GPA "+str(i[2]))

相关问题 更多 >

    热门问题