Nikhil Prabhu Nikhil Prabhu - 4 months ago 28
Python Question

Checking if a list of tuples is a subset of another

Firstly, note that I have gone through the other list-subset questions and they are not related to my problem here.

I have two lists

>>> l1 = [[(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked')]]
>>>
>>> l2 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked'), (9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]


I'm trying to check if one is a subset of another.

But before that I checked out the results of subtracting one list from another and I got disappointing results -

>>> [word for word in l1 if word not in l2]
[[(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked')]]

>>> [word for word in l2 if word not in l1]
[(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked'), (9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]


Why am I getting identical lists as my results?
Does this have something to do with the fact that they are tuples?

Answer

The problem is that l1 is a list of list of tuple (i.e. [[tuple]]), whereas l2 is a list of tuple (i.e. [tuple]). If you change this the output from the list comprehensions are what you expect:

l1 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked')]
l2 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked'), (9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]
​
a = [word for word in l1 if word not in l2]
b = [word for word in l2 if word not in l1]
​
print a
print b

[]
[(9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]
Comments