![](/img/trans.png)
[英]How to print a sentence pulling information from a dictionary that has a dictionary inside
[英]How to print a sentence pulling information from a dictionary that has a list inside
我有以下字典。 如果我想打印如下的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]
}
你可以試試這個。
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]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.