【问题标题】:need to get dictionary that is a list of other dictionaries需要获取作为其他字典列表的字典
【发布时间】:2018-11-13 11:43:22
【问题描述】:

我不知道从哪里开始检索所有字典。

【问题讨论】:

标签: python python-3.x dictionary


【解决方案1】:

你可以试试这个方法:

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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2018-01-19
    • 2013-08-22
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    相关资源
    最近更新 更多