gpt4 book ai didi

python - 我可以更优雅地打印列表数据吗?

转载 作者:太空宇宙 更新时间:2023-11-03 14:18:53 27 4
gpt4 key购买 nike

是否有更简洁的方式来构建我的打印功能?

lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}

students = [lloyd, alice, tyler]

for student in students:
print student["name"]
print student["homework"]
print student["quizzes"]
print student["tests"]

我尝试了以下代码,但出现语法错误:

for student in students:
print student["name", "homework", "quizzes", "tests"]

如果这个问题已经得到回答,我深表歉意,但我找不到问题。

最佳答案

我会使用第二个 for 循环:

for student in students:
for x in ("name", "homework", "quizzes", "tests"):
print student[x]

关于python - 我可以更优雅地打印列表数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30491159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com