简体   繁体   中英

Python find element from list of dict in other list of dict

I have two list of dict.

    students = [{'lastname': 'JAKUB', 'id': '92051048757', 'name': 'BAJOREK'}, 
{'lastname': 'MARIANNA', 'id': '92051861424', 'name': 'SLOTARZ'}, {'lastname':
 'SZYMON', 'id': '92052033215', 'name': 'WNUK'}, {'lastname': 'WOJCIECH', 'id':
 '92052877491', 'name': 'LESKO'}]

And

house = [{'id_pok': '2', 'id': '92051048757'}, {'id_pok': '24', 'id': '92051861424'}]

How to find elements that not exist in house list of dict matching by id ?

Output

output = [{'lastname':
 'SZYMON', 'id': '92052033215', 'name': 'WNUK'}]

I try do that

for student in students:

    for home in house:

        if student['id'] != home['id']:

            print student

But this only repeat list

The reason your code doesn't work is that if there's any house_id which doesn't match a student_id , the student will be printed. You'd need some more logic or the any function:

for student in students:
    if not any (student['id'] == home['id'] for home in house):
        print(student)

It outputs:

{'lastname': 'SZYMON', 'id': '92052033215', 'name': 'WNUK'}
{'lastname': 'WOJCIECH', 'id': '92052877491', 'name': 'LESKO'}

A more efficient solution would be to keep a set of house_ids, and find students whose id isn't included in this set:

students = [{'lastname': 'JAKUB', 'id': '92051048757', 'name': 'BAJOREK'},
{'lastname': 'MARIANNA', 'id': '92051861424', 'name': 'SLOTARZ'}, {'lastname':
 'SZYMON', 'id': '92052033215', 'name': 'WNUK'}, {'lastname': 'WOJCIECH', 'id':
 '92052877491', 'name': 'LESKO'}]

house = [{'id_pok': '2', 'id': '92051048757'}, {'id_pok': '24', 'id': '92051861424'}]

house_ids = set(house_dict['id'] for house_dict in house)
result = [student for student in students if student['id'] not in house_ids]

print(result)

It outputs:

[{'lastname': 'SZYMON', 'id': '92052033215', 'name': 'WNUK'}, {'lastname': 'WOJCIECH', 'id': '92052877491', 'name': 'LESKO'}]

Note that 2 students match your description.

The reason set enter link description here is used is that it allows much faster lookup than a list.

student_ids = set(d.get('id') for d in students)
house_ids = set(d.get('id') for d in house)

ids_not_in_house = student_ids ^ house_ids
students = [{'lastname': 'JAKUB', 'id': '92051048757', 'name': 'BAJOREK'}, 
{'lastname': 'MARIANNA', 'id': '92051861424', 'name': 'SLOTARZ'}, {'lastname':
 'SZYMON', 'id': '92052033215', 'name': 'WNUK'}, {'lastname': 'WOJCIECH', 'id':
 '92052877491', 'name': 'LESKO'}]

house = [{'id_pok': '2', 'id': '92051048757'}, {'id_pok': '24', 'id': '92051861424'}]

s = {item['id'] for item in students}
h = {item['id'] for item in house}

not_in_house_ids = s.difference(h)
not_in_house_items = [x for x in students if x['id'] in not_in_house_ids]
print (not_in_house_items)

>>>[{'name': 'WNUK', 'lastname': 'SZYMON', 'id': '92052033215'}, {'name': 'LESKO', 'lastname': 'WOJCIECH', 'id': '92052877491'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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