[英]How to print dictionary from list “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]
}
students = ["lloyd","alice","tyler"]
#Accessing Dictionary from List
print students[0]['name']
Expected out is : Lloyd 预期结果是:劳埃德
whenever I run the above code I get this error, 每当我运行上述代码时,都会出现此错误,
===============================================
Traceback (most recent call last):
File "python", line 30, in <module>
TypeError: string indices must be integers
===============================================
Any help will be highly appreciated. 任何帮助将不胜感激。
it's because students contains just strings ["lloyd","alice","tyler"] 这是因为学生仅包含字符串[“ lloyd”,“ alice”,“ tyler”]
print students[0]
>> "lloyd"
maybe you wanted to do this: 也许您想这样做:
students = [lloyd]
then 然后
print students[0]['name']
should work 应该管用
You may want to place the dictionaries in the list rather than just the string names. 您可能希望将字典而不只是字符串名称放在列表中。
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 "}
tyler = {"name": "Tyler "}
students = [lloyd, alice, tyler]
print students[0]['name']
Run the following command: 运行以下命令:
print students[0]
It will give your desired output. 它将给出您想要的输出。
Currently, students
is just a list of strings. 目前,
students
只是一个字符串列表。 So the value of students[0]
is a string, 'lloyd'
, which is distinct from the dictionary you created with the name lloyd
. 因此,
students[0]
的值是字符串'lloyd'
,与您创建的名称为lloyd
的字典不同。
I think you want students
to be a list of dictionaries. 我想您希望
students
成为字典列表。 Below I've shown how to do that, and created empty dictionaries for alice
and tyler
. 下面,我展示了如何执行此操作,并为
alice
和tyler
创建了空字典。 Then simply take the quotes away from your declaration of students
to store the dictionaries instead of strings with the same names, and your last line works. 然后,简单地将引号从您的
students
声明中students
以存储字典,而不是存储具有相同名称的字符串,最后一行就可以了。
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"}
tyler = {"name": "Tyler"}
students = [lloyd,alice,tyler]
print students[0]['name']
which gives: 这使:
Lloyd
You are not using the dictionary lloyd defined at the top. 您没有使用顶部定义的字典lloyd。 When you create your list with students = ["lloyd","alice","tyler"], you are instead using the string "lloyd".
当使用students = [“ lloyd”,“ alice”,“ tyler”]创建列表时,您将使用字符串“ lloyd”。 So students[0] returns this string instead of the dict.
因此,students [0]返回此字符串而不是dict。
You need to use students = [lloyd,alice,tyler]. 您需要使用学生= [劳埃德,阿里斯,泰勒]。 You might also want to create a dict for alice and tyler.
您可能还想为alice和tyler创建一个字典。
What you have tried is almost correct. 您尝试过的几乎是正确的。 You got an error because "string indices must be integers".
因为“字符串索引必须是整数”,所以出现错误。 So you can use eval() method in python ( python-eval-method ).
因此,您可以在python( python-eval-method )中使用eval()方法。
try the following code 试试下面的代码
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
students = ["lloyd","alice","tyler"]
#Accessing Dictionary from List
print eval(students[0])['name']
It will work 会工作的
your students
list contains just str
objects. 您的
students
列表仅包含str
对象。
print students[0]
>> "lloyd"
as you can see we got str
object type. 如您所见,我们得到了
str
对象类型。
you should fix it with this usage and removal of the the quotes: 您应该使用此用法并删除引号来解决此问题:
students = [lloyd]
print students[0]['name']
Done ! 完成! :)
:)
Even better to use the get
method for dict usage: 最好使用
get
方法用于dict用法:
print students[0].get('name')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.