【发布时间】:2018-11-13 11:43:22
【问题描述】:
我不知道从哪里开始检索所有字典。
【问题讨论】:
-
你有一个字典列表。字典是一个 Python 对象。如果您想提取这些对象中的第一个,那么您将访问列表中第一个位置的对象。这样做,
gradebook[0]
标签: python python-3.x dictionary
我不知道从哪里开始检索所有字典。
【问题讨论】:
gradebook[0]
标签: python python-3.x dictionary
你可以试试这个方法:
gradebook=[{"name": "Samantha", "homework": 95, "test": 92, "exam": 100},{"name": "john", "homework": 55, "test": 11, "exam": 12},{"name": "shane", "homework": 5, "test": 1, "exam": 2}]
#if you want to access first student entire dict then
print(gradebook[0])
#if you want to access any student entire dict by name
name='john'
for i in gradebook:
for index_no,value in i.items():
if value==name:
print(i)
输出:
{'exam': 100, 'test': 92, 'name': 'Samantha', 'homework': 95}
{'exam': 12, 'test': 11, 'name': 'john', 'homework': 55}
【讨论】: