Python 如何从字典列表中调用字典值?

Python 如何从字典列表中调用字典值?,python,list,dictionary,Python,List,Dictionary,我正试图通过codeacademy学习python。 作业是为每个学生制作3本字典,然后列出3本字典。然后,我应该打印出列表中的所有数据 我试着用lloyd[values]字典本身的方法调用这些值,但它说值没有定义。我也试着“打印名称”,但错误信息是我没有打印其中一个值 我非常感谢你的帮助 lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0,

我正试图通过codeacademy学习python。 作业是为每个学生制作3本字典,然后列出3本字典。然后,我应该打印出列表中的所有数据

我试着用lloyd[values]字典本身的方法调用这些值,但它说值没有定义。我也试着“打印名称”,但错误信息是我没有打印其中一个值

我非常感谢你的帮助

 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 names in students:
     print lloyd[values]
所以学生们是一个字典列表。那么你想要什么

for student in students:
    print student['name']
另外,当你想在字典中调用一个键时,你必须把键名放在引号中,作为一个字符串:alice[Homegram]不起作用,因为Python认为Homegram是一个变量。你需要的是爱丽丝[家庭作业]

因此,为了更好地查看所有信息,您可以

for student in students:
    for field in student.keys():
        print "{}: {}".format(field, student[field])

您可以进行变通以使格式更精确,例如,先打印姓名,在每个新学员之间插入新行等。

您只需打印dicts的值:

for names in students:
   print names #names are the dictionaries
如果只想打印名称,请使用名称键:


如果要打印每个学生的所有信息,必须循环遍历学生和字典中存储的值:

students = [lloyd, alice, tyler]
for student in students:
    for value in student:
        print value, "is", student[value]
import pprint
for student in students:
    pprint.pprint(student)
但是,请注意,字典不是按顺序排列的,因此值的顺序可能不是您想要的。在这种情况下,使用值的名称作为字符串作为键单独打印它们:

for student in students:
    print "Name is", student["name"]
    print "Homework is", student["homework"]
    # same for 'quizzes' and 'tests'
最后,您还可以使用pprint模块漂亮地打印学生词典:

students = [lloyd, alice, tyler]
for student in students:
    for value in student:
        print value, "is", student[value]
import pprint
for student in students:
    pprint.pprint(student)

为了可读性和可伸缩性,我建议使用namedtuple:

from collections import namedtuple

Student = namedtuple('Student', ['name', 'hw', 'quiz', 'test'])

Alice = Student('Alice', herHWLst, herQuizLst, herTestLst)
Ben = Student('Ben', hisHWLst, hisQuizLst, hisTestLst)

students = [Alice, Ben]

for student in students:
    print student.name, student.hw[0], student.quiz[1], student.test[2] 
    #whatever value you want
如果您确实想创建大量词典,可以通过以下方式使用上面的代码阅读:

for student in students:
    name = student['name']
    homeworkLst = student['homework']
    # get more values from dict if you want
    print name, homeworkLst

在Python中,访问字典的速度非常快,但创建字典的速度和效率可能不高。在这种情况下,命名的元组更实用。

这就是我在codeacademy上解决我的问题的方法。希望有人觉得这有帮助 对于学生中的学生:

print student['name']
print student['homework']
print student['quizzes']
print student['tests']

这就是我让他们接受的

for name in students:
    print name["name"]
    print name["homework"]
    print name["quizzes"]
    print name["tests"]
这也行,但他们不会接受

for name in students:
    print name

价值没有定义;这不是一个可以传递给劳埃德[…]的变量。你是说名字,价值观吗?我想他不是这么问的。