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]
}

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/

相关文章:

python - 在elasticsearch中截断索引

python - 区分两个字典键集生成结果字典

python - numpy masked_array 掩码更改类型

python - 将 Python 方法转换为 C 扩展的好处?

python - 使用 .replace() 将邮政编码转换为 pandas 列中的城市

python - 如何防止多对多类链接到相同的对象?

python - Windows 上 Python 中的长路径

python - 在多索引数据框中添加缺失日期索引

python - 如何解码中文文本中的unicode

python - 了解引发 RemoteDisconnected ("Remote end closed connection"