I am using Python 2.5.4. From here: Python -Intersection of multiple lists?
I have this:
def intersect(*d):
sets = iter(map(set, d))
result = sets.next()
for s in sets:
result = result.intersection(s)
return result
The following works as expected:
intersect([1,2,3,4], [2,3,4], [3,4,5,6,7])
But, I have something that looks more like the following:
d=[ [1,2,3,4], [2,3,4], [3,4,5,6,7] ]
If I call it like:
intersect(d)
I get:
TypeError: list objects are unhashable
How do I transform the d above into something intersect() can take?
You need to pass the contents of your list as separate parameters:
What happens otherwise is that the whole list is being used as one set instead. The
*dsyntax indicates to Python that you want to usedas a sequence of parameters to the function, instead using the wholedlist as just one parameter.