Python 我如何从字典列表中调用字典值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17755859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do i call dictionary values from within a list of dictionaries ?
提问by user1476390
i am trying to learn python with codeacademy. the assignment was make 3 dictionaries (for each student) and then make a list of the 3 dictionaries. then, i am supposed to print out all the data in the list.
我正在尝试通过 codeacademy 学习 python。作业是制作 3 部词典(为每个学生),然后列出 3 部词典。然后,我应该打印出列表中的所有数据。
i tried to call out the values the same way i used for a dictionary by itself (lloyd[values]), but then it says that values is not defined o_O. I have also tried to 'print names' but then the error message is that i didn't print out one of the values.
我试图以与我用于字典本身相同的方式调用这些值(lloyd[values]),但随后它说值未定义为 o_O。我也尝试过“打印名称”,但错误消息是我没有打印出其中一个值。
i'd appreciate your help very much.
我非常感谢你的帮助。
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]
采纳答案by tobias_k
If you want to print all the information for each student, you have to loop over the students and the values stored in the dictionaries:
如果要打印每个学生的所有信息,则必须遍历学生和字典中存储的值:
students = [lloyd, alice, tyler]
for student in students:
for value in student:
print value, "is", student[value]
Note, however, that dictionaries are not ordered, so the order of the values might not be the way you want it. In this case, print them individually, using the name of the value as a string as key:
但是请注意,字典没有排序,因此值的顺序可能不是您想要的方式。在这种情况下,单独打印它们,使用值的名称作为字符串作为键:
for student in students:
print "Name is", student["name"]
print "Homework is", student["homework"]
# same for 'quizzes' and 'tests'
Finally, you could also use the pprint
module for "pretty-printing" the student dictionaries:
最后,您还可以使用该pprint
模块“漂亮地打印”学生词典:
import pprint
for student in students:
pprint.pprint(student)
回答by A.Wan
So students
is a list of dictionaries. You then want
students
字典列表也是如此。然后你想要
for student in students:
print student['name']
Also when you want to call a key in a dictionary you have to put the key name in quotes, as a string: alice[homework]
doesn't work because Python thinks homework
is a variable. You need alice['homework']
instead.
此外,当您想调用字典中的键时,您必须将键名放在引号中,作为字符串:alice[homework]
不起作用,因为 Python 认为homework
是一个变量。你需要alice['homework']
代替。
So to look at all of the information nicely, you could do
所以要很好地查看所有信息,你可以这样做
for student in students:
for field in student.keys():
print "{}: {}".format(field, student[field])
You can play around to make the formatting nicer, e.g. printing name first, inserting a new line between each new student, etc.
您可以尝试使格式更好,例如先打印姓名,在每个新学生之间插入新行等。
回答by jh314
You can simply print the values of the dicts:
您可以简单地打印字典的值:
for names in students:
print names #names are the dictionaries
If you want to print just the names, then use the name
key:
如果您只想打印名称,请使用以下name
键:
for student in students:
print student['name']
回答by Mai
I would suggest using namedtuple instead for readability and scalability:
我建议使用 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
If you really want to create tons of dictionaries, you can read it with your code above by:
如果你真的想创建大量的字典,你可以用上面的代码阅读它:
for student in students:
name = student['name']
homeworkLst = student['homework']
# get more values from dict if you want
print name, homeworkLst
Accessing dictionary is super fast in Python, but creating them may not be as fast and efficent. The namedtuple is more practical in this case.
在 Python 中访问字典非常快,但创建它们可能没有那么快和有效。在这种情况下,namedtuple 更实用。
回答by McStar
this is how I solved mine on codeacademy. Hope someone finds this helpful for student in students:
这就是我在 codeacademy 上解决我的问题的方法。希望有人发现这对学生有帮助:
print student['name']
print student['homework']
print student['quizzes']
print student['tests']
回答by lutok
This is what I got them to accept.
这是我让他们接受的。
for name in students:
print name["name"]
print name["homework"]
print name["quizzes"]
print name["tests"]
This also works but they would not accept.
这也有效,但他们不会接受。
for name in students:
print name