我有一个名为的名称列表names
。我还有 2 个字典,其中包含一个嵌套字典列表,这些字典都具有names
与名称关联的键和其他数据。我想要做的是检查列表中的名称是否在两个字典之一中,如果是,则打印仅与该名称关联的数据。我在 python 文档中找不到任何这些东西
names = ['barry','john','george','sarah','lisa','james']
dict1 = {'results':[{'name':'barry','gpa':'2.9','major':'biology'},
{'name':'sarah','gpa':'3.2','major':'economics'},
{'name':'george','gpa':'2.5','major':'english'}]}
dict2 = {'staff':[{'name':'john','position':'Lecturer','department':'economics'},
{'name':'lisa','position':'researcher','department':'physics'},
{'name':'james','position':'tutor','department':'english'}]}
for x in names:
if x in dict1:
print gpa associated with the name
elif x in dict2:
print position associated with the name