简体   繁体   English

从另一个列表中的一个列表中找到项目?

[英]Finding an item from one list in another list?

I'm working on file parsing with Python, and I've created two dictionaries to represent two different CSV files. 我正在使用Python进行文件解析,并且创建了两个字典来表示两个不同的CSV文件。 I'm trying to take the ID number from one list, find out if it's present in the second list, and (if it is) return a copy of the record from the second list and store it in a third list. 我正在尝试从一个列表中获取ID号,找出第二个列表中是否存在该ID号,(如果存在)从第二个列表中返回记录的副本,并将其存储在第三个列表中。

For example: 例如:

List1:
[{ ID: 1, FirstName: John, LastName: Smith},
{ ID: 2, FirstName: Bob, LastName: Dole},
{ ID: 3, FirstName: Elizabeth, LastName: Jenkins}]

List2:
[{ ID: 1, HireDate: '08/24/1997' },
{ ID: 2, HireDate: '09/27/2016' },
{ ID: 17, HireDate: '01/13/2013' }]

I'm trying to say: "If you see the ID from List1 in List2, return the entry for List 2." 我试图说:“如果您在列表2中看到列表1中的ID,请返回列表2的条目。”

In other words, give me back a new list called "ID_found" and append: 换句话说,给我返回一个名为“ ID_found”的新列表并追加:

[{ ID: 1, HireDate: '08/24/1997' },
 { ID: 2, HireDate: '09/27/2016' }]

Right now, I'm trying something like: 现在,我正在尝试类似的方法:

ID_found = []
for d in List1:
    for p in List2:
        if d["ID"] in List2:
            ID_found.append(p)

I know this has a really simple solution, but I've tried everything I can think of, and nothing seems to work. 我知道这是一个非常简单的解决方案,但是我已经尝试了所有可以想到的方法,似乎没有任何效果。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks! 谢谢!

You appear to have parsed the CSV file into a list (of dictionaries), not a dictionary. 您似乎已将CSV文件解析为一个列表(词典),而不是一个词典。 To have fast lookup of ids in list 2, create an actual dictionary that maps ids to entries from list 2 as follows: 要在列表2中快速查找ID,请创建一个实际的字典,将ID映射到列表2中的条目,如下所示:

dict2 = {e['ID']: e for e in List2}

This uses a dict comprehension. 这使用了dict理解。 Then, use that to look up the ids from list 1: 然后,使用它来查找清单1中的ID:

ID_found = [dict2[d['ID']] for d in List1 if d['ID'] in dict2]

This is a list comprehension where only elements are included for which d['ID'] in dict2 is true, ie the id is present in the keys of the dictionary, which are the ids in the second list. 这是一个列表 d['ID'] in dict2 ,其中仅包含d['ID'] in dict2为true的d['ID'] in dict2 ,即,id在字典的键中存在,即第二个列表中的id。

Firstly, your dictionary mapping was incorrect and I corrected them. 首先,您的字典映射不正确,我已对其进行了更正。 You cannot just name a variable without converting it to string. 您不能只命名变量而不将其转换为字符串。

List1=[{'ID': 1, 'FirstName': 'John', 'LastName': 'Smith'},
{'ID': 2, 'FirstName': 'Bob', 'LastName': 'Dole'},
{'ID': 3, 'FirstName': 'Elizabeth', 'LastName': 'Jenkins'}]

List2=[{ 'ID': 1, 'HireDate': '08/24/1997' },
{'ID': 2, 'HireDate': '09/27/2016' },
{'ID': 17, 'HireDate': '01/13/2013' }]

common_ids = set([i['ID']for i in List1])&set([i['ID']for i in List2]) # get id values, convert them to set, do intersection operation
ID_FOUND = [j for i in common_ids for j in List2 if j['ID'] == i] # iterate and add match one to the list

Output: 输出:

C:\Users\bagiy\Documents>py test.py
[{'ID': 1, 'HireDate': '08/24/1997'}, {'ID': 2, 'HireDate': '09/27/2016'}]
  1. Get a list of unique ids from List1 List1获取唯一ID的列表
  2. Filter List2 by the ids from step 1 按步骤1中的ID过滤List2

ie

ids = set([d['ID']for d in List1])
ID_found = [d for d in List2 if d['ID'] in ids]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM