简体   繁体   English

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

[英]How to print a sentence pulling information from a dictionary that has a list inside

I have the following dictionary.我有以下字典。 If I want to print the output such as the following, how should I write it in python?如果我想打印如下的output,我应该怎么写在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]
}

You can try this.你可以试试这个。

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

Here is the working solution这是工作解决方案

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]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM