我有字典列表
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"]]
关于python - 在python中获取字典的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66084867/