如何检查列表项是否在字典中。如果是,请打印与其关联的数据

时间:2012-07-16 21:58:05

标签: python dictionary

我有一个名为names的名单。我还有2个字典,其中包含嵌套字典列表,这些字典都具有键names以及与名称关联的其他数据。我想要做的是检查列表中的名称是否在2个词典中的一个中,如果是,则打印仅与该名称关联的数据。我在python docs中找不到任何这些东西

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

4 个答案:

答案 0 :(得分:1)

for _name in names:
    if _name in [person['name'] for person in dict1['results']]: pass
    elif _name in [person['name'] for person in dict2['staff']]:pass
至少

这样的东西

答案 1 :(得分:1)

您用于两个dict的结构不是最优的 - 每个结构只包含一个元素,这是每个人的相关数据列表。如果您可以使用名称作为键重新构造每个人使用单独元素的元素,这将成为一个微不足道的问题。

dict1 = {'barry': {'gpa':'2.9','major':'biology'},
         'sarah': {'gpa':'3.2','major':'economics'},
         'george': {'gpa':'2.5','major':'english'}}

dict2 = {'john': {'position':'Lecturer','department':'economics'},
         'lisa': {'position':'researcher','department':'physics'},
         'james': {'position':'tutor','department':'english'}}

由于看起来您无法以此格式获取数据,因此您必须将其转换为:

dict_results = dict((d['name'], d) for d in dict1[results])
if name in dict_results:
    print dict_results[name]['gpa']

答案 2 :(得分:1)

这应该可以让你知道:

for name in names:
  print name, ":"
  print "\t", [x for x in dict2["staff"] if x["name"] == name]
  print "\t", [x for x in dict1["results"] if x["name"] == name]

打印

barry :
  []
  [{'major': 'biology', 'name': 'barry', 'gpa': '2.9'}]
john :
  [{'department': 'economics', 'position': 'Lecturer', 'name': 'john'}]
  []
george :
  []
  [{'major': 'english', 'name': 'george', 'gpa': '2.5'}]
sarah :
  []
  [{'major': 'economics', 'name': 'sarah', 'gpa': '3.2'}]
lisa :
  [{'department': 'physics', 'position': 'researcher', 'name': 'lisa'}]
  []
james :
  [{'department': 'english', 'position': 'tutor', 'name': 'james'}]
  []

如果从数据库获取此数据,您可能应该更好地处理问题的SQL前沿。为这样的操作建立了一个数据库。

答案 3 :(得分:0)

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'}]}


import itertools

for x in itertools.chain(dict1['results'], dict2['staff']):
    for n in names:
        if n in x.values():
            print x

或第二部分可能是:

ns = set(names)
for x in itertools.chain(dict1['results'], dict2['staff']):
    if x['name'] in ns:
        print x