I’m new to Python (working in 2.7), and I am finding SO to be a very valuable resource!
Let’s say I’m working with several lists of 2-element tuples generally of the form (ID, value), e.g.,
list1 = [(111, 222), (111, 333), (111, 444)]
list2 = [(555, 333), (555, 444), (555, 777)]
list3 = [(123, 444), (123, 888), (123, 999)]
What I really want to do is find an easy (and computationally efficient) way to get the intersection of the 2nd elements of these tuples. I’ve looked in the Python docs and found that sets might do what I want… and this post has been helpful in helping me understand how to get the intersection of two lists.
I understand that I could make three whole new “values-only” lists by looping through the tuples like this:
newList1 = []
for tuple in list1:
newList1.append(tuple[1])
newList2 = []
for tuple in list2:
newList2.append(tuple[1])
newList3 = []
for tuple in list3:
newList3.append(tuple[1])
and then get the intersection of each pair like this:
i_of_1and2 = set(newList1).intersection(newList2)
i_of_1and3 = set(newList2).intersection(newList3)
i_of_2and3 = set(newList1).intersection(newList3)
But my lists are a bit large – like hundreds of thousands (sometimes tens of millions) of tuples. Is this really the best way to go about getting the intersection of the 2nd elements in these three lists tuples? It seems…inelegant…to me.
Thanks for any help!
You are showing a large problem to begin with
variable1is generally a bad sign – if you want to have multiple values, use a data structure, not lots of variables with numbered names. This stops you repeating your code over and over, and helps stop bugs.Let’s use a list of lists instead:
Now we want to get only the second element of each tuple in the sublists. This is easy enough to compute using a list comprehension:
And then, we want the intersections between the items, we use
itertools.combinations()to get the various pairs of two possible:So, if we wrap this together:
Which gives us:
The change I made here was to make the inner list a set comprehension, to avoid creating a list just to turn it into a set, and using a generator expression rather than a list comprehension, as it’s lazily evaluated.
As a final note, if you wanted the indices of the lists we are using to generate the intersection, it’s simple to do with the
enumerate()builtin:Which gives us:
Edit:
As noted by tonyl7126, this is also an issue that could be greatly helped by using a better data structure. The best option here is to use a dict of user id to a set of product ids. There is no reason to store your data as a list when you only need a set, and are going to convert it to a set later, and the dict is a much better solution for the type of data you are trying to store.
See the following example:
Giving us: