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']
预计是:Lloyd
每当我运行上面的代码时,我都会得到这个错误,
===============================================
Traceback (most recent call last):
File "python", line 30, in <module>
TypeError: string indices must be integers
===============================================
我们将不胜感激任何帮助。
最佳答案
这是因为学生只包含字符串 ["lloyd","alice","tyler"]
print students[0]
>> "lloyd"
也许你想这样做:
students = [lloyd]
然后
print students[0]['name']
应该可以
关于python - 如何从列表 "Python": 打印字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43047011/