Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python set intersection with object sets

I am working with amazon boto and I have 2 lists. List 1 contains Instance objects. List 2 contains InstanceInfo objects. Both objects have a attribute called id. I need to get a list of Instance objects which id exists in the InstanceInfo list.

l1 = [Instance:i-04072534, Instance:i-06072536, Instance:i-08072538, Instance:i-0a07253a, Instance:i-e68fa1d6, Instance:i-e88fa1d8, Instance:i-ea8fa1da, Instance:i-ec8fa1dc]

l2 = [InstanceInfo:i-ec8fa1dc, InstanceInfo:i-ea8fa1da, InstanceInfo:i-e88fa1d8, InstanceInfo:i-e68fa1d6]

Wanted result:

l3 = [Instance:i-ec8fa1dc, Instance:i-ea8fa1da, Instance:i-e88fa1d8, Instance:i-e68fa1d6]

Right now I have it working through:

l3= []
for a in l1  
    for b in l2:
        if a.id == b.id:
            l3.append(a)

However, I have been told that I should replace this using set intersection. I have been looking at examples and it looks very straightforward. Yet I don't see any examples working with objects.

I've been playing around for a bit and theoretically I can see it work, yet there might be some 'advanced' syntax that I may not know off. I am still learning python.

like image 886
Kevin de Boer Avatar asked Mar 28 '12 12:03

Kevin de Boer


People also ask

How do you use intersection in sets?

The intersection of sets can be denoted using the symbol '∩'. As defined above, the intersection of two sets A and B is the set of all those elements which are common to both A and B. Symbolically, we can represent the intersection of A and B as A ∩ B.

Which operator is used to intersect two sets Python?

The intersection of two sets is the set of all the common elements of both the sets. You can use the intersection() method of the & operator to find the intersection of a Python set.


1 Answers

Here is something faster than Marcin's answer (while being similar):

ids_l1 = set(x.id for x in l1)  # All ids in list 1
intersection = [item for item in l2 if item.id in ids_l1]  # Only those elements of l2 with an id in l1

It is important to pre-calculate ids_l1 and to not write if item.id in set(…), as the set would be reconstructed each time (as the full test expression is re-evaluated for each element item).

Python sets give you fast element membership tests (in). Such tests are much faster with sets than with lists (as the elements of a list must be read one by one, whereas the elements of a set are "hashed").

like image 109
Eric O Lebigot Avatar answered Sep 29 '22 11:09

Eric O Lebigot