python - 在python中获取字典的一部分

标签 python arrays list dictionary

我有字典列表

teachers = [

    {
        "id": 0,
        "goals": ["travel", "relocate", "study"],
    },

    {
        "id": 1,
        "goals": ["travel","study"],
    },
    {
        "id": 2,
        "goals": ["travel", "work", "study"],
    },
    {
        "id": 3,
        "goals": ["work", "relocate", "study"],
    }
]

我需要获取字典中“工作”位于“目标”列表中的部分:

teachers = [
    {
        "id": 2,
        "goals": ["travel", "work", "study"],
    },

    {
        "id": 3,
        "goals": ["work", "relocate", "study"],
    }
]

我应该如何解决我的问题?

最佳答案

最紧凑的解决方案是使用列表理解

teachers = [t for t in teachers if "work" in t["goals"]]

List comprehension in python, how to?

关于python - 在python中获取字典的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66084867/

相关文章:

python - 如何类型提示 Python 函数返回从父类(super class)派生的任何类的实例?

python - django - 将列表转换回查询集

Java:Collections.sort 对属性不起作用?

python - 将 bool 字段从父类更改为子类 - Odoo v8

python - 在 Basemap 上绘制文本字符串代替 Python 中的点

arrays - 如何在 F# 中添加两个数值数组

c++ - 更改函数以使用 vector 而不是数组

python - 在Python中使用嵌套for循环加载字典

python - 使用时间戳列表选择由 DatetimeIndex 索引的 Pandas DataFrame 的子集

java - 用索引排序效率高吗