gpt4 book ai didi

python - 使用字典时多次打印输出

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

我正在尝试运行此 Python 代码:

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:
for key in lloyd:
print lloyd[key]
for key in alice:
print alice[key]
for key in tyler:
print tyler[key]

接收到的输出正在打印字典的关键数据的多个值。请建议我是否可以更改任何内容,以及如何使用 for 循环打印单独的关键数据,例如测试标记。

最佳答案

对于像'tests'这样的单个键,您可以只使用索引:

>>> for student in students:
... print student['tests']
...
[75.0, 90.0]
[89.0, 97.0]
[100.0, 100.0]

关于python - 使用字典时多次打印输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768149/

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