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

标签 python

我正在尝试运行此 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/

相关文章:

python - 正则表达式 - 用句点替换行首的所有空格

python - 如何在 Django 中实现多值属性?

Python: 'import *' 与 execfile

python - 为什么这段代码中的第二个列表打印了三次?

python - 查询没有命名直通字段的多对多关系

Python ImportError for strptime in spyder for windows 7

python - 修复 curve_fit 中的拟合参数

Python只对序列中的子序列进行排序

java - 在java中读取进程的实时输出

python - python 中的waveplay 等效命令