【发布时间】:2021-11-06 14:09:17
【问题描述】:
我正在尝试用 Python 开发一个小脚本,但我从未使用过这种语言。
我的结构是这样的:
dict = [
{ "id" : 1,
"people" : [ { "name" : "Sarah",
"surename" : "something"
},
{ "name" : "Luke",
"surename" : "something"
},
{ "name" : "Chris",
"surename" : "something"
}
]
},
{ "id" : 2,
"people" : [ { "name" : "Jhon",
"surename" : "something"
},
{ "name" : "Luke",
"surename" : "something"
},
{ "name" : "Ronald",
"surename" : "something"
}
]
}
]
我还有另一个值列表,例如name_list = ["Sarah", "Luke"]。
我需要找到结构的所有 ID,以便 name_list 中的所有名称都出现在字典列表 people 中。
我尝试过类似的方法,但这不起作用。
for person in dict:
if all(name_list in p["name"] for p in person["people"]):
# Do something with person["id"]
找到包含 所有 name_list 名称的字典列表的所有 ID 对我来说很重要。
【问题讨论】:
标签: python python-3.x list dictionary