1

我有一个名为的名称列表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
4

4 回答 4

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

至少是这样的

于 2012-07-16T22:04:41.043 回答
1

您为两个dicts 使用的结构不是非常理想 - 每个都只包含一个元素,它是每个人的相关数据的列表。如果您可以使用名称作为键对每个人使用单独的元素进行重组,那么这将成为一个微不足道的问题。

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']
于 2012-07-16T22:14:50.430 回答
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 前沿。为这样的操作创建了一个数据库。

于 2012-07-16T22:18:25.680 回答
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
于 2012-07-17T07:47:16.710 回答